在我正在尝试开发的游戏中,我试图拖放精灵。这是我目前的代码:
-(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的方法,即使你只是触摸精灵。我需要告诉我的代码不要做任何事情,除非拖动精灵但我无法弄清楚如何去做。我在这里缺少什么?
答案 0 :(得分:0)
所以,在稍微搞乱我的代码后,我解决了我的问题。确实是因为我的代码混乱了。所以这就是它现在的样子:
//I declared a boolean called _spriteTouch at the top of my class
-(id)init
{
//Added this to my init method
_spriteTouch = FALSE;}
-(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(touchflag == 1)
{
//set _spriteTouch to true then set the position of the sprite
_spriteTouch = TRUE;
[selectedSprite setPosition:location];
}}}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(gameStatus == TRUE)
{
//I removed the NSSet *touch, CGPoint location etc since I no longer need it here. Keep it if you're gonna use it somewhere in this method
//Check if _spriteTouch is true, if it is then the sprite would do what it should accordingly
if(_spriteTouch == TRUE)
{
if(selectedSprite != nil)
{
//do something
}
selectedSprite = nil;
}
_spriteTouch = FALSE;
}
}
所以这就是它。我还删除了endTouch,beginTouch,dragSprite等,因为我不再需要它们了。我希望这有助于其他人。