Dlost1

If velocity than

Recommended Posts

Hello,

I am creating a small platformer.

I want my npc enemy-sprites to move into the other direction when they collide with a block of layer4. However the "if' statement does not work. Which is weird cause speed/velocity often checks as 0 ( see lowest code snippet from runntime). 

So: How do I make the If statement in the do-function fire????

(uploaded it to github : https://github.com/dijoluc/DungeonDouble )

FYI: the update function is part of the Game1.js and the do function is part of the npc.js file.

In update function

game.physics.arcade.collide(badguys, this.layer3);
//calling the do function
game.physics.arcade.collide(badguys, this.layer4, npc.do(this.badguys), null, this );
do: function(objects){

//badguys = the group of ennemies
badguys.forEach(function(element) {

//debugging
console.log(element.body.velocity.x);

//set speed to 50
element.body.velocity.x = 50;


if(element.body.velocity === 0 || element.body.velocity === -0){

//reverse speed to move in opposite direction
element.body.onCollide = element.body.velocity.x *= -1;
}

My debugging/development console screen:

From my developerstool while running

Phaser CE v2.7.3 | Pixi.js | WebGL | WebAudio     http://phaser.io ♥♥♥

3   VM1498:15 0     // 3 times an element/object was checked to have 0 speed

101 VM1498:15 50    // 101 times an element/object was checked to have 50 speed
    VM1498:15 0     // 1 time an element/object was checked to have 0 speed
                   
5   VM1498:15 50    // 5 times an element/object was checked to have 50 speed
    VM1498:15 0
                    // 1 time an element/object was checked to have 0 speed
5   VM1498:15 50    // 5times an element/object was checked to have 50 speed
    VM1498:15 0

5   VM1498:15 50    // 5times an element was checked to have 50 speed
    VM1498:15 0     // 1 time an element/object was checked to have 0 speed

5   VM1498:15 50    // 5times an element was checked to have 50 speed


So it does check elements for 0 velocity...so why does my If not fire??

Share this post


Link to post
Share on other sites

I am not an expert in JS, but isn't do a reserved word that must not be used as a label? Maybe the problem lies in there?

What I also noticed, should you not check for a component of the velocity? Velocity and speed are two different things. Velocity is a vector property, so it has an x and y property. You should check for the speed if you want the velocity as a magnitude ( a single value):

http://phaser.io/docs/2.4.4/Phaser.Physics.Arcade.Body.html#speed

https://www.w3schools.com/js/js_reserved.asp

Share this post


Link to post
Share on other sites

samid737 is right on the money with velocity being a vector property. 

The if statement is failing because it's checking to see if the element.body.velocity == 0, which will never be true because element.body.velocity is an object that (roughly) looks like 

{
  X: 50,
  Y: 0
}

And that object will never === 0. 

A good way to debug this would be to console.log both values you're comparing to see what they actually are. Or you could use the debugger in the browser if you know how. 

I'm not sure what the right way to accomplish your goal is, but this is the crux of your issue. 

Share this post


Link to post
Share on other sites

Thanks!

I am still learning JS...I forgot about the reserved keyword do:) Thanks. 

Furher I am gonna try checking bot x and y. I ll get back to this topic and let you know the result!

Share this post


Link to post
Share on other sites

I dived a bit into your source code (ikr too much time on my hands) and looked for what you are trying to achieve.

 

You are doing alot of things that Phaser can easily do for you without the code you provided. If I understand you correctly you want your skeleton/spinal enemies (which look awesome by the way) to reverse their direction when colliding against the cubes. You can let Phaser do all the work for you by adding this in your create function after you enable their bodies:


game.physics.arcade.enable(badguys);
badguys.forEach(function(badguy){badguy.body.bounce.x=1});

//or
//badguys.setAll('body.bounce.x',1);

This will let phaser bounce your objects into the opposite direction after collision.

your update should then look like this:

game.physics.arcade.collide(badguys, this.layer4,npc.reverse);

Then to reverse your sprite direction you can replace your do (I called it reverse here) function only by this:

reverse: function(objects){
	objects.scale.x*=-1;
}

and finally you call your enemy function in the create to give them the initial velocity:

 npc.enemy(this.badguys);

The project in a zip (I removed the images folder due to upload size restriction)

dungeon.zip

Share this post


Link to post
Share on other sites
26 minutes ago, symof said:

Fiddle to the rescue.

http://jsfiddle.net/ya22nbdr/3/

So your code should look something like this:


do: function(objects){

objects.body.velocity.x = -50;
}

So as your "do" function is the Collision handler function you only need to get the object who's direction you want to change.

Hi:) I tried that before, but problem is that I did like this :

x______S_____x

the x are both small collision tile blocks. 

The ___ is a tilefloor

The S is a skeleton on patrol between the two x.

When hitting the x the direction should change . 

Your idea only sends him 1 way...sadly:S

Share this post


Link to post
Share on other sites
6 minutes ago, samid737 said:

I dived a bit into your source code (ikr too much time on my hands) and looked for what you are trying to achieve.

 

You are doing alot of things that Phaser can easily do for you without the code you provided. If I understand you correctly you want your skeleton/spinal enemies (which look awesome by the way) to reverse their direction when colliding against the cubes. You can let Phaser do all the work for you by adding this in your create function after you enable their bodies:



game.physics.arcade.enable(badguys);
badguys.forEach(function(badguy){badguy.body.bounce.x=1});

This will let phaser bounce your objects into the opposite direction after collision.

your update should then look like this:


game.physics.arcade.collide(badguys, this.layer4,npc.reverse);

Then to reverse your sprite direction you can replace your do (I called it reverse here function only by this):


reverse: function(objects){
	objects.scale.x*=-1;
}

and finally you call your enemy function in the create to give them the initial velocity:


 npc.enemy(this.badguys);

 

Thank you for the effort! I am gonna try your suggestions.

You are right, I figured there is lots more Phaser can do. But, I did follow courses in Java. I am pretty novice to JS and Phaser seemed a nice platform to learn more about JS frameworks and a bit of JS while making a fun game;) 

Iam still learnig to use the framework/libraries the right way:) Thanks for the help! I ll let you know if it works in a bit. First I need a 30 minutes break lol

Share this post


Link to post
Share on other sites
1 minute ago, Dlost1 said:

Hi:) I tried that before, but problem is that I did like this :

x______S_____x

the x are both small collision tile blocks. 

The ___ is a tilefloor

The S is a skeleton on patrol between the two x.

When hitting the x the direction should change . 

Your idea only sends him 1 way...sadly:S

do: function(objects){

objects.body.velocity.x *= -1;
}

:)

So if body.velocity = 50, on collion it would be 50*-1 = -50, then if it collides again its -50 * -1 = 50.

The issue was that you were setting 

//set speed to 50
element.body.velocity.x = 50;

inside the collision handler . so it's always going to start at 50. You need to set the velocity outside of the collision handler and simply reverse it when it collides. Or use the bounce function that @samid737 provided as an example.

Share this post


Link to post
Share on other sites

Hooohoo! It works! Thanks samid737 and all others who helped me out.

Now I also understand why spritesheets often have images facing only one direction. With objects.scale.x*=-1; it gets swapped with ease.

Not sure if I am allowed to say this here but I got the sprites from opengameart, Samid (since you liked ehm). 

@Symof Thanks I went for the bounce but you gave me some more insight in the js side.

Happy it works but still feeling slightly frustrated that I didn't find a solution myself;) A well on to shooting fireballs and such. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.