Cocos2d Spritebuilder移动精灵和射击弹药

时间:2014-04-06 03:50:02

标签: objective-c cocos2d-iphone

我正在浏览sprite构建器教程,并尝试更改内容以查看它是如何工作的,我让自己陷入困境并需要帮助。 这是我的代码 这些问题标有//< ----

  1. 我试图根据您按下按钮的时间让小角色在屏幕上左右移动。 我通过搜索找到的最好的是下面但不像按住按钮和移动角色那样顺利。

  2. 所有操作似乎都是在结束时触摸,而不是触摸(开始)时,我该如何修改?

    static const CGFloat scrollSpeed = 150.f;
    @implementation MainScene{
    CCNode *hero;
    CCPhysicsNode *physicsNode;
    }
    
    - (void)didLoadFromCCB {
    self.userInteractionEnabled = TRUE;
    }
    
    - (void)update:(CCTime)delta {
    hero.position = ccp(hero.position.x + delta * scrollSpeed, hero.position.y);
    physicsNode.position = ccp(physicsNode.position.x - (scrollSpeed *delta), physicsNode.position.y);
    }
    
    - (void)jump {//make the hero jump (still deciding on 400 or 600)
    [hero.physicsBody applyImpulse:ccp(0, 400.f)];
    }
    
    - (void)fire {  
    CCNode* ammo = [CCBReader load:@"Ammo"];
    ammo.position = ccpAdd(hero.position, ccp(10, 20));
    // add the ammo to the physicsNode of this scene (because it has physics enabled)
    [physicsNode addChild:ammo];
    
    // manually create & apply a force to launch the knife
    CGPoint launchDirection = ccp(1, 0);
    CGPoint force = ccpMult(launchDirection, 8000);
    [ammo.physicsBody applyForce:force];
    }
    
    - (void)left { <---- How can I make the hero move based on how long the button is pressed?
    CGPoint moveDirection = ccp(-1, 0);
    CGPoint force = ccpMult(moveDirection, 8000);
    [hero.physicsBody applyForce:force];
    }
    
    - (void)right { <---- How can I make the hero move based on how long the button is     pressed?
    CGPoint moveDirection = ccp(1, 0);
    CGPoint force = ccpMult(moveDirection, 8000);
    [hero.physicsBody applyForce:force];
    }
    
    - (void)retry {
    // reload this level
    [[CCDirector sharedDirector] replaceScene: [CCBReader loadAsScene:@"MainScene"]];
    }
    - (void)exit {
    CCScene *menuScene = [CCBReader loadAsScene:@"MenuScene"];
    [[CCDirector sharedDirector] replaceScene:menuScene];
    }
    
    @end
    

0 个答案:

没有答案