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:
Update Script
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:
Update Script
if(this.hitTest(player)){
if(player.x>this.x){
player.x+=7;
}else{
player.x-=7;
}
}
Here’s the code for the horizontal floor:
Update Script
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.
is it just me or is this WAY harder.
I think I’ll stick to this:
if (this.hitTest(player){
player.axis -/+ speed
}
player = player
axis = x/y
-/+ = -/+
speed = the player’s speed