我在cocos2d
中的游戏正在以只获取触摸
但不获取滑动/拖动
移动的方式处理用户的触摸。如何在 cocos2d/objective-c
中控制滑动/拖动
移动?
例如如果用户在 sprite_player 上方屏幕的任何点滑动手指,我想将 sprite_player 向上移动。
这是我现在正在处理触摸的函数:
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInNode:self];
positionY1 = self.contentSize.height*.2;
positionY2 = self.contentSize.height*.40;
positionY3 = self.contentSize.height*.60;
float distY1Y2 = (positionY1 + positionY2)/2;
float distY2Y3 = (positionY2 + positionY3)/2;
float playerMove;
if (touchLocation.y <= distY1Y2) {
if(_player.position.y <= distY1Y2) {
playerMove = positionY1;
} else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
playerMove = positionY1;
} else if (_player.position.y >= distY2Y3) {
playerMove = positionY2;
}
} else if (touchLocation.y > distY1Y2 && touchLocation.y < distY2Y3) {
if(_player.position.y <= distY1Y2) {
playerMove = positionY2;
} else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
playerMove = positionY2;
} else if (_player.position.y >= distY2Y3) {
playerMove = positionY2;
}
} else if (touchLocation.y >= distY2Y3) {
if(_player.position.y <= distY1Y2) {
playerMove = positionY2;
} else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
playerMove = positionY3;
} else if (_player.position.y >= distY2Y3) {
playerMove = positionY3;
}
}
//play audio
[[OALSimpleAudio sharedInstance] playEffect:@"pew-pew-lei.caf"];
//Disable another touch movement
//[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
// Move our sprite to touch location
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:.2f position:CGPointMake(_player.position.x, playerMove)];
[_player runAction:actionMove];
//Enable another touch movement
//[[UIApplication sharedApplication] endIgnoringInteractionEvents];
/*
NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(enableTouch:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];
[inv setTarget: self];
[inv setSelector:@selector(enableTouch:)];
[NSTimer scheduledTimerWithTimeInterval:.7f
invocation:inv
repeats:NO];
*/
}
最佳答案
我将分享一些代码。请仔细阅读此代码。
您的问题:如果用户在 sprite_player 上方屏幕的任何点滑动手指,我想将 sprite_player 向上移动。
答:
这是为了,如果您触摸特定的 Sprite 并移动该 Sprite ......
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint point=[myTouch locationInView:[myTouch view]];
point=[[CCDirector sharedDirector] convertToGL:point];
NSLog(@"point is %@",NSStringFromCGPoint(point));
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint point=[myTouch locationInView:[myTouch view]];
point=[[CCDirector sharedDirector] convertToGL:point];
CCNode *Sprite=[self getChildByTag:spriteTag]; // This is your sprite, which one you want to move.
[Sprite setPosition:point];
// This is the boundary for sprite, for what sprite is nor cross the screen.
if(Sprite.position.y>245)
[Sprite setPosition:ccp(Sprite.position.x,245)];
if(Sprite.position.y<25)
[Sprite setPosition:ccp(Sprite.position.x,25)];
if(Sprite.position.x<15)
[Sprite setPosition:ccp(15,Sprite.position.y)];
if(Sprite.position.x>465)
[Sprite setPosition:ccp(465,Sprite.position.y)];
}
这是为了,如果您触摸屏幕上的任意位置并移动该 Sprite ......
// CGPoint initialPos; // in .h file
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPt = [touch locationInView:touch.view];
initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
我想这会对你有帮助:)
关于ios - 处理幻灯片运动cocos2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23600420/