cocos2d
中的我的游戏正在以一种只获得touches
但不是slide/drag
次移动的方式处理来自用户的触摸。我怎样才能slide/drag
中cocos2d/objective-c
的{{1}}移动?
E.g。如果用户在sprite _player上方的屏幕的任何位置滑动手指,我想向上移动精灵_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];
*/
}
答案 0 :(得分:1)
我会分享一些代码。请仔细阅读此代码。
你的问题:如果用户在精灵_player上方的屏幕的任何一点滑动手指,我想向上移动精灵_player。
答:这是为了,如果你触摸特定的精灵并移动那个精灵......
-(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)];
}
如果您触摸屏幕上的任意位置并移动该精灵......
// 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)];
}
我认为这会对你有所帮助:)。