Jump to content

Pushable Object?


Cutu
 Share

Recommended Posts

Hello guys this is my first post in this forum I hope you can help me.

I was wondering how to make a pushable object? I'm working with ARCADE physics and my game is a shooting/platformer game. I have this enemy on the top who is hanging in a rope like a monkey trying to get down to kill the player. The player has a slingshot that shoot rocks to make the monkey go up until reach X = 0.

So first I would like to make the monkey go up when the rock hit it, but just like 10units up while he keeps falling to kill the player, the problem is that when I use the collide function the monkey takes the force of the rock and it bounces to the top, i just want it to move a little, then stop and keep falling, if you shoot him many times he'll get to the top eventually and you win the game. I could only make the bullet to dissapear when it collide.

Sorry for my bad english, and thanks!

Link to comment
Share on other sites

With moving up and down I assume you mean Y instead of X.

Anyway, what you can do is setting the vertical velocity of the monkey to negative for a short moment after being hit.

So let's say your monkey is moving down with speed 10 over time (monkey.body.velocity.y = 10;), you can do the following in your collision handler, where you also make the bullet disappear:

//Set vertical speed to negative to move up
monkey.body.velocity.y = -20;
//Change vertical speed back after a short delay (in ms, so 1000 = 1 second)
game.time.events.add(1000, function () {
   monkey.body.velocity.y = 10;
}, this);

You may have to tweak the velocity/time values a bit to get the distance you'd like to have :).

Link to comment
Share on other sites

2 hours ago, Taggrin said:

With moving up and down I assume you mean Y instead of X.

Anyway, what you can do is setting the vertical velocity of the monkey to negative for a short moment after being hit.

So let's say your monkey is moving down with speed 10 over time (monkey.body.velocity.y = 10;), you can do the following in your collision handler, where you also make the bullet disappear:


//Set vertical speed to negative to move up
monkey.body.velocity.y = -20;
//Change vertical speed back after a short delay (in ms, so 1000 = 1 second)
game.time.events.add(1000, function () {
   monkey.body.velocity.y = 10;
}, this);

You may have to tweak the velocity/time values a bit to get the distance you'd like to have :).

Alright, that's exactly what I meant, it works, but the problem is that when I shoot 3 rocks at the same time after the first rock hits the monkey and pushes him in the Y position it makes like a force that stop in the collide position the other 2 rocks instead of keep pushing him up. This is the function that I have inside my update().

function update() {
        playerUpdate();
        if(game.input.activePointer.isDown){
            fireRock();
        }
        bossUpdate();
    }

function bossUpdate() {
        monkey.body.velocity.y = 10;
        this.game.physics.arcade.collide(rocks,monkey,this.rockMonkey,null,this);
        
    }

    function rockKing(rocks,monkey) {
        //If I put rocks instead of monkey it kills the monkey, do the kill() function mean that whatever it collide with it will get killed?

        monkey.kill();
        monkey.body.velocity.y = -30;
        game.time.events.add(1000, function() {
            //This works but as I said it when 2 rocks or more touch the monkey it make the second collide with an invisible wall at the same Y spot it was before de 1st rock touches him and doesn't dissapear.
            monkey.body.velocity.y = 10;
        });
    }

Thank you!

Link to comment
Share on other sites

You can try to switch game.physics.arcade.collide with game.physics.arcade.overlap, as that will remove the speed blocking of objects. It shouldn't matter for gameplay, as you kill the bullets upon touch anyway.

This collision handler will not work perfectly for multiple bullet hits. Instead of tweaking the velocity you can add a number to a counter inside the collision handler. In the time.events you can reduce this number after a second. As long as the counter is higher than 0 the monkey should still go up. This also allows you to multiply the velocity with the counter, making the monkey go up faster if multiple hits happen in quick succession. Not necessary, but something you could consider.

Also keep in mind that you should set the initial speed of 10 in the create() function. You have it in the bossUpdate(), which is part of update(), overwriting your collision handler.

 

 

Link to comment
Share on other sites

Hey guys, sorry I was working yesterday and couldn't reply, @Taggrin I followed your advice by setting the initial speed of the monkey in the create() function and changed the collide handler with overlap and now it works perfectly, the only problem that I have now is that when you hit the monkey in the "quick succession" of rocks he won't be affected by the second bullet, or the third until he finishes going up -Y.

Do you have any suggestion?

Almost forgot to ask something that I can't understand. I have this function:

function rockMonkey(rocks,monkey) {
        counterRM += 1.5;
        monkey.kill();
        if (counterRM > 0){
        rocks.body.velocity.y = -15 * counterRM;
        } else {
            rocks.body.velocity.y = -15;
        }
        game.time.events.add(300, function() {
            counterRM -= 1;
            rocks.body.velocity.y = 10;
        });
    }

Which is called in the update() function to handle the overlap, my question is, why do I have to call the monkey.kill() function to kill the rocks? Isn't supposed to be rocks.kill()? or the kill() function is supposed to kill the object that collides with it? the same thing happens when I want to change the velocity of the monkey, as you can see I had to tweak the rocks velocity to affect the monkey's one. This is confusing me, or maybe I'm doing something wrong? but at least it's working.

Thanks in advance!

PD: @samme I haven't tried your suggestion, will try it later :).

Link to comment
Share on other sites

13 hours ago, Cutu said:

The only problem that I have now is that when you hit the monkey in the "quick succession" of rocks he won't be affected by the second bullet, or the third until he finishes going up -Y.

Do you have any suggestion?

You have most you require already in your code. Using the counterRM value you can check how many bullets did hit it recently so you can multiple the monkey.body.velocity.y with that value. However, this will increase the to ridiculous amounts if you hit too many times in a row. If you want it to maintain the same speed, but that it just lasts for longer, you need to remove the game.time.events and add your own timer. This way you can increase that timer after a successful hit.

Something like this (sorry, for some reason the codebox doesn't want to display Javascript):

//Define your timer somewhere in the CREATE function
monkeyTimer = 0;



//This should go into the UPDATE function
//It resets the monkey speed once the timed event is over
if (monkeyTimer < game.time.now) {
   monkey.body.velocity.y = 10;
}



//This should go into the collision handler
//First it will change the speed
//Then it adds time to the current time (e.g. 1 second) to determine a future point in time
monkey.body.velocity.y = -25;
monkeyTimer = game.time.now + 1000;

Because the monkeyTimer gets updated on every hit, the monkey will keep moving up after hits until you stop doing that. Because if the monkeyTimer does not get updated anymore, the current time will eventually surpass it and reset the monkey speed in a downward direction.

 

As for the overlap kill issue: I have no idea. I used these kinds of systems many times before and the way you expect it to work does actually for me. Maybe you flipped a value in the overlap or something? Hopefully someone else has an answer to this.

 

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...