Ios 精灵套件:如何制作节点“;“跳跃”;还可以根据节点位置和触摸长度向左或向右移动?

Ios 精灵套件:如何制作节点“;“跳跃”;还可以根据节点位置和触摸长度向左或向右移动?,ios,sprite-kit,Ios,Sprite Kit,这是我的第一篇帖子,所以放松点,哈哈 我不熟悉“iOS”编码、“Xcode”和“spritekit”。如果我触摸屏幕上的任何地方,我希望使图像节点在y轴正方向上“跳跃”一段距离,但如果我触摸图像并保持一定时间,则图像节点向左或向右移动,距离与触摸长度相对应 不知道这是否很清楚,但如果有任何帮助,我们将不胜感激!谢谢 请尝试以下代码: 精灵移动:根据触摸方向在屏幕上任意位置向左/向右移动 // CGPoint initialPos; // in .h file -(void)ccTouc

这是我的第一篇帖子,所以放松点,哈哈

我不熟悉“iOS”编码、“Xcode”和“spritekit”。如果我触摸屏幕上的任何地方,我希望使图像节点在y轴正方向上“跳跃”一段距离,但如果我触摸图像并保持一定时间,则图像节点向左或向右移动,距离与触摸长度相对应

不知道这是否很清楚,但如果有任何帮助,我们将不胜感激!谢谢

请尝试以下代码:

精灵移动:根据触摸方向在屏幕上任意位置向左/向右移动

  // CGPoint initialPos; // in .h file

  -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
     UITouch *touch = [touches anyObject];
     CGPoint touchPt = [touch locationInView:touch.view];
     initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
  }
  -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
     UITouch *myTouch=[touches anyObject];
     CGPoint currentPos=[myTouch locationInView:[myTouch view]];
     currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];

     float diffX = currentPos.x - initialPos.x;
     float diffY = currentPos.y - initialPos.y;

     CGPoint velocity = ccp(diffX, diffY);
     initialPos = currentPos;
     [Sprite setPosition:ccpAdd([SmileBall position], velocity)];
  }

  - (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  {
     UITouch *myTouch=[touches anyObject];
     CGPoint currentPos=[myTouch locationInView:[myTouch view]];
     currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
     float diffX = currentPos.x - initialPos.x;
     float diffY = currentPos.y - initialPos.y;
     CGPoint velocity = ccp(diffX, diffY);
     initialPos = currentPos;
     [Sprite setPosition:ccpAdd([SmileBall position], velocity)];
  }
  -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
     CCSprite *RunningChar;
     RunningChar=(CCSprite *)[self getChildByTag:10];
     UITouch *touch = [touches anyObject];
     CGPoint touchPt = [touch locationInView:touch.view];
     touchPt = [[CCDirector sharedDirector] convertToGL:touchPt];

     float radian = ccpToAngle(ccpSub(touchPt, prevPoint));
     float degrees = CC_RADIANS_TO_DEGREES(radian);

     if (degrees >= 22.5 && degrees <= 112.5) // for upward direction :). otherwise you can write only code without ant condition
     {
         CCJumpTo *jump=[CCJumpTo actionWithDuration:0.7 position:CGPointMake(RunningChar.position.x, 87) height:110 jumps:1];
         [RunningChar runAction:seq];
     }
     else
     {

     }
  }
精灵跳跃:根据您在屏幕上的触摸

  // CGPoint initialPos; // in .h file

  -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
     UITouch *touch = [touches anyObject];
     CGPoint touchPt = [touch locationInView:touch.view];
     initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
  }
  -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
     UITouch *myTouch=[touches anyObject];
     CGPoint currentPos=[myTouch locationInView:[myTouch view]];
     currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];

     float diffX = currentPos.x - initialPos.x;
     float diffY = currentPos.y - initialPos.y;

     CGPoint velocity = ccp(diffX, diffY);
     initialPos = currentPos;
     [Sprite setPosition:ccpAdd([SmileBall position], velocity)];
  }

  - (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  {
     UITouch *myTouch=[touches anyObject];
     CGPoint currentPos=[myTouch locationInView:[myTouch view]];
     currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
     float diffX = currentPos.x - initialPos.x;
     float diffY = currentPos.y - initialPos.y;
     CGPoint velocity = ccp(diffX, diffY);
     initialPos = currentPos;
     [Sprite setPosition:ccpAdd([SmileBall position], velocity)];
  }
  -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
     CCSprite *RunningChar;
     RunningChar=(CCSprite *)[self getChildByTag:10];
     UITouch *touch = [touches anyObject];
     CGPoint touchPt = [touch locationInView:touch.view];
     touchPt = [[CCDirector sharedDirector] convertToGL:touchPt];

     float radian = ccpToAngle(ccpSub(touchPt, prevPoint));
     float degrees = CC_RADIANS_TO_DEGREES(radian);

     if (degrees >= 22.5 && degrees <= 112.5) // for upward direction :). otherwise you can write only code without ant condition
     {
         CCJumpTo *jump=[CCJumpTo actionWithDuration:0.7 position:CGPointMake(RunningChar.position.x, 87) height:110 jumps:1];
         [RunningChar runAction:seq];
     }
     else
     {

     }
  }
-(void)CCTouchsMoved:(NSSet*)触摸事件:(UIEvent*)事件
{
CCSprite*RunningChar;
RunningChar=(CCSprite*)[self-getChildByTag:10];
UITouch*touch=[触摸任何对象];
CGPoint touchPt=[touch locationInView:touch.view];
touchPt=[[CCDirector sharedDirector]convertToGL:touchPt];
浮动弧度=ccptoagle(ccpSub(touchPt,prevPoint));
浮动度=CC_弧度到_度(弧度);

如果(度>=22.5&°rees,则可以像这样移动节点

在touchesEnded:或ToucheSbegind:方法中:

{
node.position.y += 50;
}
为了让精灵移动到某个地方,您也可以使用动作,有一系列动作,如
moveTo
action


我建议你选择一本关于sprite kit的教程或一本书。免费教程的好网站是raywenderlich.com

请分享你尝试过的内容。我真的没有做太多,就像我说的,我对objective c编码非常新鲜。我尝试了一种touchesbeagin方法,它可以增加节点向上移动的冲动,但不确定如何才能做到o通过触摸等使其向左向右移动。尝试Ray Wenderlich的网站。他有一个名为Super Koalio的教程,可以做任何你想做的事情。tut只需要一点时间。它还可以帮助你在他的网站上学习obj c、xcode的东西和大量的好信息。有很多错误。没有已知的“sharedDirector”类方法“使用未声明的标识符'initialPos'”在哪里出现错误,请分享。我将解决您的问题:)我在代码中提到请CGPoint initialPos;//在.h文件中:)