gpt4 book ai didi

ios - 沿滑动方向移动 Sprite

转载 作者:行者123 更新时间:2023-11-29 04:12:36 25 4
gpt4 key购买 nike

我想沿着用户滑动的方向移动我的 Sprite ( spaceship )。IE,向左移动 - 向左滑动向上移动 - 向上滑动等.

我是滑动编码新手,所以请帮忙。

最佳答案

这样就可以了:

 - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{


if (touchState != kSceneStateUngrabbed) return NO;

// Store the starting touch point
CGPoint touchPoint = [touch locationInView:[touch view]];
startTouchPoint = touchPoint;

touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];




// Change touch state
touchState = kSceneStateGrabbed;

return YES;
}



- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint = [touch locationInView:[touch view]];




float rightleft = touchPoint.x-startTouchPoint.x ;
float updown = touchPoint.y-startTouchPoint.y;
if(abs(rightleft)>abs(updown)){
if(rightleft>0)
{
[ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(winsize.width, spaceShip.position.y)]];

}
else
[ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(0, spaceShip.position.y)]];
}
else{
if(updown<0){
[ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(spaceShip.position.x, winsize.height)]];
}
else
[ spaceShip runAction:[CCMoveTo actionWithDuration:1 position:ccp(spaceShip.position.x, 0)]];

}

}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
// Change touch state
touchState = kSceneStateUngrabbed;


}

关于ios - 沿滑动方向移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14196033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com