簡體   English   中英

iPhone,Cocos2D-在觸摸屏幕時向左/​​向右移動精靈

[英]iPhone, Cocos2D - moving sprite left/right while touching screen

我是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

我對此是否正確? 目前,它在更新中給了我“未使用的表達式結果”。 誰能告訴我我想念的東西嗎? 任何幫助將不勝感激。

謝謝,帕特里克

我在這里看到一些東西:

  • 不確定您的選擇器將調用update:@selector(update :)
  • 我不會依靠dt精確地等於1/60秒,也不會保持不變。 我希望定義一個速度常數(以每秒點數為單位),並在每個更新周期基於所需速度和dt計算以點為單位的deltaX。
  • 我沒有看到“ registerWithTouchDispatcher”調用(我嘗試將它們放在onEnter和onExit中)方法。
  • 確保在其中的某個位置刪除子級(在dealloc中,或者在本地清理方法中更好(不要忘記調用[super cleanup]))。

刪除更新函數中的參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM