我正在製作一個涉及在屏幕上從左到右移動的太空船的iPhone遊戲。我想這樣做,只有按下按鈕,船隻纔會移動。這是我創建移動的當前代碼,但當按鈕不再按下時,它不會停止。COCOS2d創建按鈕時的動作
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGSize winSize = [CCDirector sharedDirector].winSize;
NSSet *allTouches = [event allTouches];
UITouch *touch = [allTouches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint([_paddle2 boundingBox], location)){
int bottomOfScreenX = 0 + _Paddle1.contentSize.width/2;
id action = [CCMoveTo actionWithDuration:5 position:ccp(bottomOfScreenX,winSize.height/3) ];
[_starShip runAction:action];
[action setTag:1];
}
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[_starShip stopActionByTag:1];
}
我個人不會打擾CCMoveTo。在按下按鈕時更新太空船的位置,並沿x軸遞增。一旦按鈕不再被按下停止遞增。 –