Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by LazerFriends

I am developing a platformer game where the player entity is able to pick up and put down crates. I have 2 issues:

1. I was able to program the player entity to pick up the crate, but only if I am pressing the forward key AND the pickup key. How can I code it so the player can pick up the crate by just standing next to it (touching) and not running against it (colliding)?

2. Simply put: how can the player put the crate back down again?

Here is the code for the pickup:

check: function(other){
if( ig.input.state('pickup')) {
other.kill();
this.kill();
ig.game.spawnEntity(EntityPlayerBox,other.pos.x, other.pos.y);
};
this.parent();
}, 

And a link to the game (Arrow keys for movement, X to pick up crates)

http://bsmallbeck.com/impact/

1 decade ago by mimik

I like your art there.

you can do something like

var player = ig.game.getEntitiesByType( EntityPlayer )[0];

// if this create distance to player is lower then 64px and pressing pickup
if (this.distanceTo( player ) < 64 && ig.input.state('pickup')){
  // do stuff
}

1 decade ago by LazerFriends

Thanks mimik! I inputed that code, and it still doesn't work...The player entity still has to be running into the crate in order for the pickup to work...

1 decade ago by jerev

Quote from LazerFriends
Thanks mimik! I inputed that code, and it still doesn't work...The player entity still has to be running into the crate in order for the pickup to work...


That's because you most likely put that code in the "check" method, instead, you should put it in the update method of the crate.

The check method will only be fired when the collision happens.

1 decade ago by LazerFriends

@jerev Of course! Thank you.

Could I use the check() method to make sure the player is facing the crate in order to pick it up, and the player w/crate is spawned facing the same direction? (Try picking up the crate with the player facing the left side to see what I'm talking about)

1 decade ago by jerev

Edit:
Just read in a different topic you're new to javascript, assuming you have not used a c-syntaxed language I might need to clarify this short hand if expression

y = (x == 5) ? 2 : 10;

Is equal as doing:
if (x == 5) {
    y = 2;
} else {
    y = 10;
}


Hey,

I peaked a little in your code, and so I'm using entity.flip instead of the long entity.currentAnim.flip.x. Essentially its the same thing, due to your code.

First of all, I'd define a method to see if the facing is right:

// GOES IN Box

facingCorrect: function(other) {
    return ((!other.flip && other.pos.x < this.pos.x) || (other.flip && other.pos.x > this.pos.x));
}

The above might be confusing, its equivalent to:
facingCorrect: function(other) {
    if (other.flip == false) { // other facing right
        if (other.pos.x < this.pos.x) { // other must be on left side
            return true;
        } else {
            return false;
        }
    } else { // other facing left
        if (other.pos.x > this.pos.x) { // other must be on right side
            return true;
        } else {
            return false;
        }
    }
}


Now, you can check if the facing is right. However, this is not all, because, you also want your new entity to spawn on the right location:

* the one of the player if the player is on the left side of the crate
* the one of the box if the player is on the right side.

Note, due to the nature of flipping, you also want to make sure the movement inputs are not pressed, if they are, you always need to use the crate location, else you'd see a little shift (I think)

// GOES IN Box

update: function(){	
    // AND statement with facing check.
    if(this.distanceTo(ig.game.player) < 38 && ig.input.state('pickup') && this.facingCorrect(ig.game.player)) {
        ig.game.player.kill();
        this.kill();

        var left = (ig.game.player.pos.x < this.pos.x), // Check if the player is on the left side of the box
        /*
            This might not be very clear, in order to see what happens, try to use just this:
                x = (!left) ? this.pos.x : ig.game.player.pos.x;
            And now pick the crate up, while holding the input keys, you should see a little shifting happening.
            This is due to the nature of flipping. (This is all from personal experience however, so I might be wrong. Not tested.)
         */
        x = (!left || ig.input.state('forward') || ig.input.state('back')) ? this.pos.x : ig.game.player.pos.x;
        

        ig.game.spawnEntity(EntityPlayerBox, x, this.pos.y, {flip: !left}); // spawn with the flip setting.
	};
	
	this.parent();
}

Again, I did not test any of that, so if its not working as expected, letme know and I'll look over it again ;)

1 decade ago by jerev

The releasing of the crate aint too hard, but there are some things to note again.
I put it all in comments, all of it goes in update method of the player+crate ofcourse.

// GOES IN PlayerBox.update //

//First time you run through the code, the this.onetick will be false, so it won't fire.
//This will prevent our code from running continous (picking up, releasing, picking up, release, ...)
if (ig.input.pressed('pickup') && this.onetick) {
    this.kill();
    var cratewidth = 38, //CHANGE THIS TO YOUR VALUE.
        x = 0;


    x = (this.flip) ? (this.pos.x+cratewidth) : this.pos.x; // Define the x coord with a simple if, and the width of the crate
    ig.game.player = ig.game.spawnEntity(EntityPlayer, x, this.pos.y, {flip: this.flip}); // Be sure to give the flip setting.

    x = (this.flip) ? this.pos.x : (this.pos.x+cratewidth); // Again define the x coord, but now reversed.
    ig.game.spawnEntity(EntityBox, x, this.pos.y);

    // call this.parent, and leave the update method. In other words, kill the entity without running needless code.
    return this.parent(); 
}
this.onetick = true; // After one run, set it to true.

Note, now we need to make a minor change to the box update (picking up code) aswell:

// CHANGES TO Box.update

if(... && this.onetick) { // same idea
...
}
this.onetick = true;

And also change the ig.input.state('pickup') into the pressed method: ig.input.pressed('pickup') (in the box update if)


--------------------------------

Again, I did not test any of the above, so there might be bugs, but the logics should be right. If not, letme know, I check this forum quite often after all.

1 decade ago by LazerFriends

Wow jerev, this is huge! I can't thank you enough!

One quick n00b question though, are the 2 pieces of code you provided supposed to be separate if statements in the update function, or integrated into one?

1 decade ago by jerev

Quote from LazerFriends
Wow jerev, this is huge! I can't thank you enough!

One quick n00b question though, are the 2 pieces of code you provided supposed to be separate if statements in the update function, or integrated into one?


Hey, updated my posts, I added a comment line in each of the code fragments to indicate more cleary what file/entity it goes in.

Additionally, I can provide you with the final files after edits - with some digging in your uploaded code - commented like above.
Perhaps try and put the pieces together yourself for better understanding though, or go over the code a couple times in order to get full understanding.

It's important you learn where things go in a game-whole. (Not saying I'm the best tutor)

https://dl.dropbox.com/u/43009917/impact/help/LazerFriends/BoxPickupRelease/player.js
https://dl.dropbox.com/u/43009917/impact/help/LazerFriends/BoxPickupRelease/box.js
Page 1 of 1
« first « previous next › last »