【问题标题】:iPhone, Cocos2D - moving sprite left/right while touching screeniPhone,Cocos2D - 在触摸屏时向左/向右移动精灵
【发布时间】:2012-01-25 17:43:49
【问题描述】:

我是 Objective C 和应用程序开发的新手,所以请放轻松! 我正在尝试制作一个基本游戏,并且需要在用户的手指在屏幕上时连续向左或向右移动精灵-左侧向左,向右向右... 我正在尝试使用更新每 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

我在这方面是否正确?目前它在更新中给了我“未使用的表达式结果”。谁能告诉我我错过了什么?任何帮助将不胜感激。

谢谢, 帕特里克

【问题讨论】:

    标签: iphone ios cocos2d-iphone schedule ccsprite


    【解决方案1】:

    我在这里看到了一些东西:

    • 不确定您的选择器会调用更新:@selector(update:)
    • 我不会依赖 dt 正好是 1/60 秒,也不会是恒定的。我倾向于在每个更新周期定义一个速度常数(以每秒点数为单位)并根据所需的速度和 dt 以点数计算 deltaX。
    • 我没有看到“registerWithTouchDispatcher”调用(我尝试将它们放在 onEnter 和 onExit 方法中)。
    • 在那里的某个地方,确保您删除了您的孩子(在 dealloc 中,或者在本地清理方法中更好(不要忘记调用 [super cleanup])。

    【讨论】:

      【解决方案2】:

      删除更新函数中的参数

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-31
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多