Hi, i am trying to add a feature to my game. What im trying to make is, when my object touches a wall the wall doesnt allow the object to move past it. The wall acts as a wall.
How do I do this?
Help
Hi, i am trying to add a feature to my game. What im trying to make is, when my object touches a wall the wall doesnt allow the object to move past it. The wall acts as a wall.
How do I do this?
Help
anyone know?
NO ONE ANSWERED THIS ONE EITHER!!! GOSH I HATE THIS FORUM. I donāt know why these people have to sit around for days just to get an answer? whatever
So I thought about this a bit. Hereās a project that has a moving character which hits walls. There is only code on the character. The one issue is that you need to name all of your walls individually, but for project borders Youāll only need 4 walls!
wallsThatPushBack-Dec7-2018-1.50PM.html (788.0 KB)
My computer wont let me view your file. Can you put the code in the comments plz.
and who is [alacazzableobeardinc)?
Try clicking on it and then drag the download into a new tab. That should solve your problem.
@Animator786, hereās the code Iāve added to the character in the example above. (Itās kind of nasty). The names of my walls are āwallā and āwall2ā.
// How much did we move last turn?
var moveSpeed = 5;
var walls = [wall, wall2]; // List the names of ALL the walls in my project.
function keyDown(key) {
// Save our old positon
var oldX = this.x;
var oldY = this.y;
// Try to move the character
if (key === 'RIGHT') {
this.x = this.x + moveSpeed; // Move 5 pixels to the right
} else if (key === 'UP') {
this.y = this.y - 5; // Move 5 pixels to the right
} else if (key === 'LEFT') {
this.x = this.x - moveSpeed; // Move 5 pixels to the right
} else if (key === 'DOWN') {
this.y = this.y + moveSpeed; // Move 5 pixels to the right
}
// If we hit a wall, move us back.
if (hitWalls(this)) {
this.x = oldX;
this.y = oldY;
}
}
// Pass in an object to see if it hit the wall.
function hitWalls(objectToCheck) {
var hitWall = false;
// Go through all of the walls I have listed.
for (var i=0; i<walls.length; i++) {
var wallToCheck = walls[i];
// If I hit the wall, let the person who called the function know.
if (objectToCheck.hitTest(wallToCheck)) {
hitWall = true;
}
}
return hitWall;
}
Hereās an image of the project, the ball is the character.
Thank you so much. This REALLY helped. its exaclty what I was looking for.
Happy to help! Sorry it took a few days to get back to you. Let me know if I can give you any additional support!
Hello
Can you please send me the project I am trying to understand how to program!
Thanks
Sure, heres a project that uses game boundaries.
New Project-Jan8-2019-10.54AM.wick (8.7 KB)
Hope this helps
For my project, It keeps going past the walls. I dont know what Im doing wrong.
This is my code:
var wall = [Wall]; // List the names of ALL the walls in my project.
function keyDown(key) {
// Save our old positon
var oldX = this.x;
var oldY = this.y;
// Try to move the character
if (key === 'RIGHT') {
this.x = this.x + moveSpeed; // Move 23 pixels to the right
} else if (key === 'UP') {
this.y = this.y - 23; // Move 23 pixels to the right
} else if (key === 'LEFT') {
this.x = this.x - moveSpeed; // Move 23 pixels to the right
} else if (key === 'DOWN') {
this.y = this.y + moveSpeed; // Move 23 pixels to the right
}
// If we hit a wall, move us back.
if (hitWalls(this)) {
this.x = oldX;
this.y = oldY;
}
}
// Pass in an object to see if it hit the wall.
function hitWalls(objectToCheck) {
var hitWall = false;
// Go through all of the walls I have listed.
for (var i=0; i<walls.length; i++) {
var wallToCheck = walls[i];
// If I hit the wall, let the person who called the function know.
if (objectToCheck.hitTest(wallToCheck)) {
hitWall = true;
}
}
return hitWall;
}
if (key === āupā){
this.y -=23;
this.rotation = 0;
}
if (key === ādownā){
this.y +=23;
this.rotation = 180;
}
if (key === āleftā){
this.x -=23;
this.rotation = -90;
}
if (key === ārightā){
this.x +=23;
this.rotation = 90;
}
Your var is
wall = [Wall]
Iām not a good coder, but I can see that you might need to add an āsā to wall, let me know if it works, or if it was just a typo you made on the forums!
Hereās how it should be:
var walls = [Wall]
Thatās interesting! @Luxapodular code is very smart and complicated for me to read, so I changed it and also played around with your code to see how I could change it .
Hereās a .wick file, if somehow you canāt access the codeās Iāve plugged in, let me know! Iād be happy to write them here, also let me know if it doesnāt work, or if you need more help with it!
making walls and objects with hitTest function tutorial (335.0 KB)
~ Hamzah Alani
this file will not let me see the code can you please type them in the reply box?
Oh, that project is a pretty old one (I got a lot better at coding right now), but if you still want the code, here it is.
thank you and also how do i make it stop moving the oposite way because if i hit test one way the try to hit test the oposite side it just cancels out and lets the character go through, and if i hit test it to make it solid or an obstacle then i try to make it update moving up it just still lets it pass through
Well, I made that project a while ago, and it has many problems. Hereās a new one with WAY fewer things and code:
My Project9-28-2020_4-43-53PM.wick (6.7 KB)
Hereās the code for the player:
var speed = 7;
if(isKeyDown('left')) {
this.x -= speed;
}
if(isKeyDown('right')) {
this.x += speed;
}
if(isKeyDown('up')) {
this.y -= speed;
}
if(isKeyDown('down')) {
this.y += speed;
}
Hereās the code for the vertical wall:
if(this.hitTest(player)){
if(player.x>this.x){
player.x+=7;
}else{
player.x-=7;
}
}
Hereās the code for the horizontal floor:
if(this.hitTest(player)){
if(player.y>this.y){
player.y+=7;
}else{
player.y-=7;
}
}
And thatās literally all. Hope this helps
thank you a lot lol Iām new this coding stuff lol and so far its very interesting thx again and have a good one.