Restrict on-screen movement
fgb01

Good morning people,

I am trying to restrict my player's movements so that they do not exceed the size of the screen ... how can I do this? With the clamp? I'm doing it with the keyboard. Ex: iskeydown ...
Thank you very much.

All 5 Comments
Gio

If your camera is not moving, then assuming you have an object called player you can do

var pos = _.player.getPosition();
var size = _.player.getSprite().getSize();
var hs = wade.vec2.scale(size, 0.5);      // half your sprite's size
var hw = wade.getScreenWidth() / 2;       // half screen width
var hh = wade.getScreenHeight() / 2;      // half screen height
pos.x = Math.min(hw - hs.x, Math.max(-hw + hs.x, pos.x));
pos.y = Math.min(hh - hs.y, Math.max(-hh + hs.y, pos.y));
_.player.setPosition(pos);

 

fgb01

Good Morning,
What part of my drive code should I add this to?
Thanks a lot for the help.

Follow the code:
 

var controles = {
    esquerda: "left",
    direita:"right"
};
var vel_jogador = 100;

if(wade.isKeyDown(controles.esquerda) &&! wade.isKeyDown(controles.direita)){
    this.setVelocity(-vel_jogador,0);
}else if(wade.isKeyDown(controles.direita) &&! wade.isKeyDown(controles.esquerda)){
    this.setVelocity(vel_jogador,0);
}else{
    this.setVelocity(0,0);
}

Gio

I would add it to your player's onUpdate function.

fgb01

It worked ... thank you

fgb01

And when is it random?
Does something change?
I am using the function in app.js:

This.getRandomInt = function (min, max) {
             Min = Math.ceil (min);
             Max = Math.floor (max);
             Return Math.floor (Math.random () * (max-min)) + min;
};
 
Already on Update I use:

Var randomx = wade.app.getRandomInt (-wade.getScreenWidth () / 2, wade.getScreenWidth () / 2);

and

This.setPosition (randomx, -250);

Where should I use it?
Thank you very much

Post a reply
Add Attachment
Submit Reply
Login to Reply