0

私のゲームでは、連続的に動くプラットフォームとプレーヤーを配置しました。プレーヤーは各プラットフォームを飛び越えて上りますが、プラットフォームは左から右に移動しており、プレーヤーが動くプラットフォームに座ると、プラットフォームもスライドします。

しかし、問題は、プラットフォームが画面の左端に衝突して右側に移動し始めると、プレーヤーがプラットフォームで停止せずに左側に向かって移動し続け、落下することです。プレイヤーの動きを止めるにはどうすればよいですか?車を壊すときにガラスを飛び越えるようなものです。:(

Initメソッドでは、プレーヤーを作成しています。CCSprite *_playerヘッダーファイルで宣言されています。

// Creates player
_player = [CCSprite spriteWithImageNamed: @"Assets.atlas/Player.png"];
[_player setPosition: ccp(160.0f, 160.0f)];

_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1
_player.physicsBody.collisionGroup = @"playerGroup"; // 2
_player.physicsBody.collisionType = @"player";
//[_physicsWorld addChild:_player];
[_foreground addChild: _player];

_foregroundプラットフォームが追加される CCNode であり、プレイヤーが上に移動している間に下にスクロールして、プレイヤーが上に移動している効果を与えることができます。

//This is how platform node is added in init method.
PlatformNode *platform3 = (PlatformNode *)[PlatformNode node];
[platform3 createPlatformAtPosition:CGPointMake(110, 50) ofType: PLATFORM_NORMAL];
[_foreground addChild: platform3];

createPlatform...スプライトと物理ボディを配置するメソッドが実装されています。に記述されているメソッドのコードを参照してくださいPlatformNode.m

- (void) createPlatformAtPosition:(CGPoint) position ofType:(PlatformType) type {
    //PlatformNode *node = [PlatformNode node];

    if (type == PLATFORM_BREAK) {
        _sprite = [CCSprite spriteWithImageNamed: @"PlatformBreak.png"];
    } else {
        _sprite = [CCSprite spriteWithImageNamed: @"Platform.png"];
    }
    [_sprite setPosition: ccp(_sprite.contentSize.width/2, _sprite.contentSize.height/2)];
    [self addChild: _sprite];

    [self setPosition: position];
    [self setName: @"NODE_PLATFORM"];
    [self setPlatformType: type];

    self.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _sprite.contentSize} cornerRadius: 0.0];
    self.physicsBody.collisionGroup = @"platform";
    self.physicsBody.type = CCPhysicsBodyTypeStatic;
    self.physicsBody.collisionType = @"obstacle";

//    if (type == PLATFORM_MOVE) {
//    // Animate projectile
//    CCActionMoveBy* actionMove = [CCActionMoveBy actionWithDuration:4.0f position: CGPointMake(-100, 0)];
//    CCActionReverse *actionRev = [CCActionReverse actionWithAction: actionMove];
//    CCActionSequence *seq = [CCActionSequence actionWithArray: @[actionMove, actionRev]];
//    [self runAction:[CCActionRepeatForever actionWithAction: seq]];
//    }

    //return self;
}

プラットフォームを左から右に連続的に移動させるために、次のロジックが実装されています。

CGPoint actPoint = platform3.position; CGPoint extPoint = ccp([CCDirector sharedDirector].viewSize.width - platform3.contentSize.width*2, platform3.position.y); CGPoint zrPoint = ccp(0, platform3.position.y);

    //CCActionMoveBy* actionMove = [CCActionMoveTo actionWithDuration:((ccpSub(actPoint, zrPoint).x * 8.0) / extPoint.x) position: CGPointMake(0, platform3.position.y)];
    CCActionMoveBy* actionMoveF = [CCActionMoveTo actionWithDuration:8.0f position: extPoint];
CCActionMoveBy* actionMoveR = [CCActionMoveTo actionWithDuration:8.0f position: zrPoint];
    CCActionSequence *seq = [CCActionSequence actionWithArray: @[actionMoveF, actionMoveR]];
//CCActionReverse *actR = [CCActionReverse actionWithAction: seq];
//CCActionSequence *act = [CCActionSequence actionWithArray: @[seq, actR]];
    [platform3 runAction:[CCActionRepeatForever actionWithAction: seq]];

誰でもここで私を助けることができますか?

4

1 に答える 1

0

問題を解決しました。ここのリンクからアイデアを得ました: Cocos2d 3.0 + Chipmunk + CCAnimation: アニメーション化されたオブジェクトで物理体を動かす。どのように?

プレイヤーが立っている地面の値を保持する一時変数を取得しました。コードは衝突検出方式で書かれています。

-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair player:(CCSprite *)player obstacle:(CCCustomSprite *)obstacle {
    // Taken the ground as temporary variable when player collides with the obstacle
    _ground = obstacle;
    return YES;
}

Now in touchBegin メソッド

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // Remove the reference from the ground as the player jumps up from the ground, needs to release the ground first in order to avoid its velocity
    _ground = nil;

    // Making a jump
    [_player.physicsBody applyImpulse: ccp(0, _player.physicsBody.mass * JUMP_IMPULSE)];
}

地面の速度と同じ速度を適用しますが、プラットフォームが水平方向にのみ移動するため、x 値のみを変更するようにしてください。

- (void) update:(CFTimeInterval)currentTime {
  if (_ground) {
        CGPoint veloc = _ground.physicsBody.velocity;
        _player.physicsBody.velocity = ccp(veloc.x, _player.physicsBody.velocity.y);
    }
}

私のためにうまく働いています。ただし、別の問題は、ジャンプの速度を上げることができないことです。たとえば、私のプレーヤーは 2 秒で 100 ポイントをジャンプします。0.5 秒でジャンプしたいのですが、ジャンプできますか?

于 2014-10-28T17:11:17.983 に答える