Jump to content

(Solved)Any tips or idea to Locking the Player on float platform?


khleug35
 Share

Recommended Posts

Hello everyone, I am beginner of panda2.

I would like to create a platformer game like Kuru Panda, so I download panda2-template-platformer.zip to start my project.

and want to create a float platform that can move right to left or left to right on game like the following demo

2:26

 

but I am not successful to create that, the player can't move when drive on the float platform

1.thumb.png.945d81076a487289912d7c1adb912d6f.png

 

2.thumb.png.b8a690bb06f7008be186799f3aad3dad.png

 

game.createClass('Float_platformer', {
    init: function() {
        this.float_platformer = new game.Sprite('float_platformer.png');
        this.float_platformer.anchorCenter();
        this.float_platformer.addTo(game.scene.container);
        
        this.body = new game.Body();
        this.body.mass = 0;
        this.body.position.x = 1260;
        this.body.position.y = 1460;
        this.body.collisionGroup = 2;
        
        
        var shape = new game.Rectangle();
        shape.width = this.float_platformer.width;
        shape.height = this.float_platformer.height;
        
        this.body.addShape(shape);
        this.body.static = true;
        this.body.addTo(game.scene.world);
        
        
        game.Tween.add(this.body.position, {
			x: 1560
		}, 2000, {
			easing: 'Quadratic.InOut',
			repeat: Infinity,
			yoyo: true
		}).start();
    },
    update: function() {
        this.float_platformer.position.x = this.body.position.x;
        this.float_platformer.position.y = this.body.position.y;
    }
    
}); 

 

game.createClass('Player', {
.....

collide2: function(other) {
        if (other.collisionGroup === 2) {
           this.body.velocity.x = 200;
        }
    },

....
)};

 

any tips or idea??? Thank you very much

Link to comment
Share on other sites

[snip player collision]
collide2: function(other) {
        if (other.collisionGroup === 2) {
           this.body.velocity.x = 200;
        }
    },

I'd probably change this to just set a flag. e.g.

this.onPlatform = true.

this.myPlatform = other; //let's store a reference to the other.

 

Then in update() you can try some tricks to move the player.

//normal move left/right code, update x coordinates, etc.

if ( this.onPlatform ) {
   if ( other !== undefined ) { 
       this.body.x += other.body.xVelocity;
   }
}

 

You could also do the update in the platform itself. Like:

update: function() {
        if ( playerObj.onPlatfrom) {
             playerObj.x += ( this.float_platformer.position.x - this.body.position.x);
        }

        this.float_platformer.position.x = this.body.position.x;
        this.float_platformer.position.y = this.body.position.y;
        
    }


 

Something like this? 

Link to comment
Share on other sites

 

Oh, Many Thank you for your help @Wolfsbane

Finally I work on it !!!!!!!! Really Really Thanks !!!!!!!!  

firstly I added this code on player class

collide: function(body, dir) {
if (body.collisionGroup === 2) {
this.onPlatform = true; // default is false
this.myPlatform = body;
}else{
this.onPlatform = false;  
} 

and add this code on float platform update function

update: function() {
if (game.scene.player.onPlatform) {
game.scene.player.body.position.x -= ( this.float_platformer.position.x - this.body.position.x); 
}

this.float_platformer.position.x = this.body.position.x;
this.float_platformer.position.y = this.body.position.y;
}

It work!!!! 

Thanks again, for the help on this , :D :D :D :D :D :D :D :D

have a nice day

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...