iPhone,Cocos2D - 在触摸屏时左/右移动精灵

时间:2022-04-07 14:35:41

I'm new to Objective C and app development so please go easy on me! I'm trying to make a basic game and need to move a sprite left or right continuously while the user's finger is on the screen - left side to go left, right to go right... I'm trying to use update to repeat movements of a few pixels every 1/60th second. So far, this is what I have (and sorry about the formatting):

我是Objective C和app开发的新手,所以请放轻松我吧!我正在尝试制作一个基本的游戏,并且需要在用户的手指在屏幕上时连续向左或向右移动精灵 - 左侧向左,向右向右...我正在尝试使用更新重复每1/60秒运动几个像素。到目前为止,这就是我所拥有的(并且对格式化感到遗憾):

    #import "GameplayLayer.h"

    @implementation GameplayLayer

    -(id)init {
         self = [super init];
         if (self != nil) {
         CGSize screenSize = [CCDirector sharedDirector].winSize;  
          // enable touches
         self.isTouchEnabled = YES;  

     blobSprite = [CCSprite spriteWithFile:@"blob.png"];
    [blobSprite setPosition: CGPointMake(screenSize.width/2, screenSize.height*0.17f)];

    ball = [CCSprite spriteWithFile:@"ball.png"];
    [ball setPosition:CGPointMake(10, screenSize.height*0.75f)];

    [self addChild:blobSprite];  
    [self addChild:ball];

    [self schedule:@selector(update) interval:1.0f/60.0f];

   }
   return self;
 }



    -(void) update:(ccTime)dt{
      if (_tapDownLeft == YES){
         blobSprite.position.x==blobSprite.position.x-5;
      }
     if (_tapDownRight == YES){
         blobSprite.position.x==blobSprite.position.x+5;
     }
    }


-(void) ccTouchesBegan:(UITouch*)touch withEvent: (UIEvent *)event{

 CGPoint touchLocation = [touch locationInView:[touch view]];
 touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];

if (touchLocation.x > 400) {
   if ((blobSprite.position.x+10)<460){
           _tapDownRight = YES;
      }
}

if (touchLocation.x < 200) {
    if ((blobSprite.position.x-10>20)){
           _tapDownLeft = YES;
      } 
}

else {
    _tapDownLeft = NO;
    _tapDownRight = NO; 
      }   
}
 -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
      _tapDownLeft = NO;
      _tapDownRight = NO;
      }



-(void) registerWithTouchDispatcher{
  [[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0                            swallowsTouches:YES];
}


@end

Am I on the right lines with this? At the moment it's giving me 'expression result unused' in update. Could anyone tell me what I'm missing? Any help would be greatly appreciated.

我是否在这条线上?目前它在更新中给了'表达结果未使用'。谁能告诉我我错过了什么?任何帮助将不胜感激。

Thanks, Patrick

2 个解决方案

#1


1  

i see a few things here:

我在这看到一些事情:

  • not certain your selector will call update : @selector(update:)
  • 不确定你的选择器会调用update:@selector(update :)

  • I would not rely on dt being either exactly 1/60th of a second, nor being constant. I would favor defining a speed constant (in points per second) and compute the deltaX in points based on the desired speed and dt, at each update cycle.
  • 我不会依赖于dt正好是1/60秒,也不是常数。我倾向于定义速度常数(以每秒点数为单位),并在每个更新周期基于所需速度和dt计算点数deltaX。

  • I dont see a 'registerWithTouchDispatcher' call (i try to place them in onEnter and onExit) methods.
  • 我没有看到'registerWithTouchDispatcher'调用(我尝试将它们放在onEnter和onExit中)方法。

  • Somewhere in there, make certain you remove your children (either in dealloc, or better in a local cleanup method (dont forget to invoke [super cleanup]).
  • 在那里的某个地方,确保你删除你的孩子(在dealloc中,或者在本地清理方法中更好(不要忘记调用[super cleanup])。

#2


0  

Remove the argument in the update function

删除更新功能中的参数

#1


1  

i see a few things here:

我在这看到一些事情:

  • not certain your selector will call update : @selector(update:)
  • 不确定你的选择器会调用update:@selector(update :)

  • I would not rely on dt being either exactly 1/60th of a second, nor being constant. I would favor defining a speed constant (in points per second) and compute the deltaX in points based on the desired speed and dt, at each update cycle.
  • 我不会依赖于dt正好是1/60秒,也不是常数。我倾向于定义速度常数(以每秒点数为单位),并在每个更新周期基于所需速度和dt计算点数deltaX。

  • I dont see a 'registerWithTouchDispatcher' call (i try to place them in onEnter and onExit) methods.
  • 我没有看到'registerWithTouchDispatcher'调用(我尝试将它们放在onEnter和onExit中)方法。

  • Somewhere in there, make certain you remove your children (either in dealloc, or better in a local cleanup method (dont forget to invoke [super cleanup]).
  • 在那里的某个地方,确保你删除你的孩子(在dealloc中,或者在本地清理方法中更好(不要忘记调用[super cleanup])。

#2


0  

Remove the argument in the update function

删除更新功能中的参数