Copy link to clipboard
Copied
Hello I am wondering is there a proper way to implement player collision with objects.
I have tried figuring this out for over a week now. I probably should have asked on here earlier.
I would rather not have to install a lib to do this if possible.
If a lib is neccissary please link me to some documentation on how to use it.
I tried nape with no luck. I am trying to make it so when I collide with an object it stops the player from moving further without skipping them back a few steps to reset them allowing them to move back away from the collision without being stuck to the collider.
you can use removeEventListener to remove an event listener, but if you're re-adding event listeners because a frame is playing it's better to use:
var alreadyExecuted;
if(!alreadyExecuted){
// any code you don't want re-executed. eg,
whatever.addEventListener(someevent,somefunction);
alreadyExecuted = true;
}
Copy link to clipboard
Copied
canvas or as3?
what problem are your having?
Copy link to clipboard
Copied
Thank you for your reply.
In my player script I check if the player is colliding here
if (varLeft == true){
if(_collision){
char.x += speed;
_collision = false;
return;
}
char.x -= speed;
}
Im simply resetting the char.x back a step if it collides with a collision object because if I dont set it back a step _collision will still be true.
I want to make it so the char will not need to be reset back a step but allow them to move away still without _collision equaling true. I hope Im making sense.
Copy link to clipboard
Copied
if you're trying to make the char reverse direction upon collision, use:
if (varLeft == true){
if(_collision){
speed *= -1;
_collision = false;
}
char.x += speed;
Copy link to clipboard
Copied
no Im trying to just make the character stop on collision and not stop them from moving completely after collision.
Copy link to clipboard
Copied
For instance. say a car crashes into a wall. obviously the car would stop from the collision.
But my problem is that once I crash into the wall I cant back away from the wall. Im stuck on the collision.
Copy link to clipboard
Copied
usually after a collision the one or both objects are distroyed or move away (eg, rebound) from each other. otherwise, you're going to have a problem.
Copy link to clipboard
Copied
could you provide me with a proper collision example the way you think it should be done.
So essentially weather the player is moved back a step or whatever, however it should be done.
Copy link to clipboard
Copied
for example I currently have
if(varRight == true){
char.x += step;
}
if(varLeft == true){
char.x -= step;
}
if(varUp == true){
char.y -= step;
}
if(varDown == true){
char.y += step;
}
if (char.collisionArea.hitTestObject(Main.world.collision)){
_collision = true;
}
for player movement. how would I make it so if _collision = true it stops or kicks the player back to their previous position.
Copy link to clipboard
Copied
var prevX = char.x;
var prevY = char.y;
if(!_collision) {
if(varRight) {
rightF()
}
if(varLeft) {
leftF();
}
if(varUp) {
upF();
}
if(varDown) {
downF()
}
} else {
if(varRight && prevMove!="right") {
rightF()
}
if(varLeft && prevMove!= "left") {
leftF();
}
if(varUp && prevMove!="up") {
upF();
}
if(varDown && prevMove!="down") {
downF()
}
}
// you may want a min distance before resetting this
_collision = false;
}
if(char.collisionArea.hitTestObject(Main.world.collision)) {
_collision = true;
char.x = prevX;
char.y = prevY;
}
function rightF(){
char.x += step;
prevMove = "right";
}
function leftF(){
char.x -= step;
prevMove = "left";
}
function upF(){
char.y -= step;
prevMove = "up";
}
function downF(){
char.y += step;
prevMove = "down";
}
Copy link to clipboard
Copied
Thank you soo much, it works perfect.
Just one last question. how would i remove the event listener for movement because it is stacking the movement speed every time I start the application for some reason.
Copy link to clipboard
Copied
you can use removeEventListener to remove an event listener, but if you're re-adding event listeners because a frame is playing it's better to use:
var alreadyExecuted;
if(!alreadyExecuted){
// any code you don't want re-executed. eg,
whatever.addEventListener(someevent,somefunction);
alreadyExecuted = true;
}