简体   繁体   English

Cocos2d iPhone Game 触屏让人物动起来

[英]Cocos2d iPhone Game touch screen to make a character move

I have written a few games using cocos2d but they were all controlled by the accelerometer and used only simple touch events.我使用 cocos2d 编写了一些游戏,但它们都由加速度计控制并且只使用简单的触摸事件。 All I need to do is register when the screen is touched, anywhere.我需要做的就是在任何地方触摸屏幕时进行注册。 I don't need any information about the position or velocity.我不需要有关 position 或速度的任何信息。 The character currently moves across the screen and the users should be able to touch to make the character move up the screen.角色当前在屏幕上移动,用户应该能够触摸以使角色在屏幕上向上移动。 the current code does not work as intended.当前代码无法按预期工作。 The character is not effected by the touch, it just continues to move down the screen.角色不受触摸的影响,它只是继续向下移动屏幕。 Please advise.请指教。 Below is the code I am trying to use now.下面是我现在尝试使用的代码。

In the game update method:游戏更新方法中:

if (IsTouched == TRUE) {
    SealPositionBasedOnTouchInt = SealPositionBasedOnTouchInt - (100*dt);
}

else {
    SealPositionBasedOnTouchInt = SealPositionBasedOnTouchInt + (100*dt);
}

SealSwimming.position = ccp(SealPositionBasedOnTouchInt, SealSwimming.position.y);

The touch events:触摸事件:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


    for( UITouch *touch in touches ) 
    {
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    IsTouched = TRUE;
}  
}

You'll notice I do get the touch location, this is currently not used for anything but was in the sample.你会注意到我确实得到了触摸位置,这目前没有用于任何事情,但在示例中。

Make sure the layer has touch events enabled isTouchEnabled确保图层启用了触摸事件isTouchEnabled

Have you overridden:你有没有覆盖:

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

in your layer/node that is receiving the touch?在您的层/节点中接收触摸?

You might also try and call from your code addTargetedDelegate:self without overriding registerWithTouchDispatcher , but I have never tried it.您也可以尝试从您的代码中调用addTargetedDelegate:self而不覆盖registerWithTouchDispatcher ,但我从未尝试过。

Sergio has the answer to your problem with touches not registering. Sergio 为您的触摸未注册问题提供了答案。 I had a similar problem recently.我最近遇到了类似的问题。 If it doesn't matter where the user touches the screen then swallowsTouches: YES is fine, but if you have a couple layers that need to register touches you might need to set it to NO.如果用户触摸屏幕的位置无关紧要,那么swallowsTouches: YES 很好,但是如果您有几个需要注册触摸的层,您可能需要将其设置为NO。

I haven't tested this.我没有测试过这个。

in .h

CCSprite *sprite;


in .m

-(id)init
{
    if ((self=[super init))
    {
           CGSize s = [[CCDirector sharedDirector] winSize];

           sprite = [CCSprite spriteWithFile:@"imagename.png"];
           sprite.position = ccp(s.width/2, s.height/2);
           [self addChild:sprite];
    }
    return self;
}

    - (void) ccTouchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{

    UITouch *touch = [touches anyObject];

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

    [sprite setPosition:touchLocation];

    //OR YOU CAN RUN AN ANIMATION TO MAKE IT LOOK LIKE IT'S WALKING
    //[sprite runAction:[CCMoveTo actionWithDuration:5 position:touchLocation]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM