【发布时间】:2013-02-13 10:31:40
【问题描述】:
在我目前正在尝试开发的游戏中,我正在尝试拖放精灵。这是我当前的代码:
-(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 方法。我需要告诉我的代码不要做任何事情,除非精灵被拖动,但我不知道该怎么做。我在这里错过了什么?
【问题讨论】:
-
检查并计算位置位移,然后将条件放在位移值上。
-
所以如果我没看错的话,你希望游戏在精灵被拖动时清除,但在精灵被触摸时结束?
-
是的,这正是现在发生的情况。我有一种预感,我的方法都乱七八糟了,但我不知道应该按什么顺序制作它们,因为如果我将拖动方法放在触摸移动方法中,如果你不这样做,游戏就不会结束拖动它,但当你这样做时,你不会看到精灵移动。它只是说游戏通关了。
-
如果不是这样,我怀疑我的 selectedSprite.position = ccp(....) 是问题的原因,但如果我尝试删除它,或者用拖动方法替换它,我获得错误的访问异常
-
@rptwsthi,我不确定我是否理解您的建议,抱歉。
标签: iphone objective-c cocos2d-iphone