I’m making a game but I cant Macke a wall
could you perhaps provide slightly more info as to what you are doing? it’d be really helpful to determine how we can help you make a wall of any sort.
like, are you making a platformer? car game? is there gravity?
Easy:
// wall default script
if(!project.walls)
project.walls = Object.create(null);
project.walls[this.uuid] = this;
// player update script
const lastX = this.x, lastY = this.y;
// movement...
const touchingAnything = Object.values(project.walls).filter(c => this.hits(c)).reduce((acc, cur) => cur || acc);
if(touchingAnything) {
this.x = lastX;
this.y = lastY;
This code does:
- Save the last ‘good’ position
- Movement code (you know how to do that)
- Calculate if touching anything
- If actually touching anything, revert to last ‘good’ position
The object created in the wall script was a Object.create(null). In a nutshell, it just prevents default methods included with the Object prototype (prototypes are weird)
The array sequence is simple:
-
Get an array with
project.walls's values (Object.values(project.walls)) -
Make a new array with the values ran through a function, which in this case does a
this.hits(that)(.map(clip => this.hits(clip))) I misspelled it as.filterin the post, filter will only include values which the function returnstrue -
Reduce the array of boolean values into a single boolean value (
.reduce((reducing, current) => current || reducing).reduceis really weird but also really good
thanks soo much! now my game works
I promise I don’t know the movement part I was first starting whith the gravity and stuff pls heeelllp
for movement? what kind of movement fo you need? for really simple movement, without gravity, just copy the code from the builtin asset. you can also use
if(isKeyDown("left")) {
this.vx -= this.speed;
}
if(isKeyDown("right")) {
this.vx += this.speed;
}
if(isKeyDown("up")) {
this.vy -= this.speed;
}
if(isKeyDown("down")) {
this.vy += this.speed;
}
//movement
this.x += this.vx;
this.y += this.vy;
this.vx *= 0.96;
this.vy *= 0.96;
initialize vx and vy to zero, and speed to whatever. i used 0.4. this makes for really smooth, satisfying motion. if there’s gravity, use a vx and vy system where you change vy each tick to make you fall, and set the vy to a negative integer to jump. then change vx to move horizontally. i hope this helps!
Also remember to Math.min(this.vx, this.maxSpeed), or you’ll keep going faster and faster and faster… BIG RIGS: Over The Road Racing has this problem, the speed at which you go back isn’t capped but is for going forward.
thanks, really
oh, i didnt know you could do that… i just did
if(this.vy <= this.whatever) {
[movement code here]
}
