在我目前正在尝试开发的游戏中,我正在尝试拖放精灵。这是我当前的代码:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//gameStat is a boolean I use for pause and play function
if(gameStat == TRUE)
{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
//Swipe Detection - Beginning point
beginTouch = location;
touchflag = 0;
for(int i = 0; i < [sprArray count]; i++)
{
CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
if(CGRectContainsPoint([sprite boundingBox], location))
{
selectedSprite = sprite;
//touchflag is an int, if its 1, it will move the sprite
//spriteTouch is a boolean for checking if the sprite's been moved
touchflag = 1;
_spriteTouch = TRUE;
break;
}
}
}}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStat == TRUE)
{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
if(_spriteTouch == TRUE)
{
if(touchflag == 1)
{
//Should move the sprite from start location x and y to end location x and y
selectedSprite.position = ccp(location.x, location.y);
}
else
{
for(int i = 0; i < [sprArray count]; i++)
{
CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
if(CGRectContainsPoint([sprite boundingBox], location))
{
selectedSprite = sprite;
touchflag = 1;
break;
}
}
}
}
}}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStat == TRUE)
{
//End point of sprite after dragged
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
endTouch = location;
posX = endTouch.x;
//Minimum swipe length
posY = ccpDistance(beginTouch, endTouch);
if(_spriteTouch == TRUE)
{
if(selectedSprite != nil)
{
if(selectedSprite.tag == 1)
{
[self gameOver];
}
else if(selectedSprite.tag == 2)
{
if(countDown > 0)
{
[self ClearGame];
}
else if(countDown == 0)
{
[self gameOver];
}
}
}
selectedSprite = nil;
}
_spriteTouch = FALSE;
}}
精灵的拖放工作正常。我的问题是,即使您只是触摸精灵,游戏也会进入 gameOver 或 ClearGame 方法。我需要告诉我的代码不要做任何事情,除非精灵被拖动,但我不知道该怎么做。我在这里想念什么?