#Need some help with a code.
91 messages · Page 1 of 1 (latest)
And you want to move diagonally when pressing, say, up and left?
That’s pretty simple then. You’ll just need four if statements in your step event
Which ones?
Pardon?
What four statements would I have to use?
/// Create Event
move_speed = 3;
/// Step Event
if (keyboard_check(vk_right)) {
x += move_speed;
}
if (keyboard_check(vk_up)) {
y -= move_speed;
}
if (keyboard_check(vk_left)) {
x -= move_speed;
}
if (keyboard_check(vk_down)) {
y += move_speed;
}
Well, something like this, pretty much
Hmm noted…
First we decide how fast we can move. I'm using 3 as just a random number here
You can make that any speed you want
Then in my step, I check if I'm holding the right arrow key. If I am, then I move right, by adding move_speed to x
And where should I type the speed?
I labelled both code blocks, you'd declare your move_speed variable in the Create Event
That's where you will declare all of your variables, after all
Got it
But yeah, note the differences in each if statement. They're all really similar, but do slightly different things
Going right adds to x, going left subtracts from x
Going down adds to y, going up subtracts from y
That should all make sense, since x is your horizontal position and y is your vertical position
So naturally, left and right should affect x, and up/down should affect y
What if I want it to add the other stuff you said like animation, acceleration etc
Well, then you'd need to give us all the details about what you'd exactly want with those things
There are a lot lot lot of different ways to add those things, after all
Each game is kinda unique so we'd need to know what kind of animations you want and what kind of acceleration you're looking for
Well. Mostly of all to make the object make an animation when moving and stop when not moving and change sprites depending where its pointing
Okay, so let's make some assumptions about that
Let's assume that you have eight sprites in total that you're working with
Four "standing still" sprites, one for each direction
And four "walking" sprites, one for each direction
Let's also assume that you want to track the direction you're facing in a variable
And maybe you want to use the standard angles for that variable, 0 for right, 1 for up, 2 for left, 3 for down
If that's all the case, then we could store all those sprites and store the direction we're facing in some variables, and use each one as needed
So I can make something like this?
“If Right_key pressed change variable to 1” and “if variable is 1 change to sprite number 1” something like that?
/// Create Event
move_speed = 3;
standing_sprites = [
/* your standing right sprite goes here*/,
/* your standing up sprite goes here*/,
/* your standing left sprite goes here*/,
/* your standing down sprite goes here*/
];
walking_sprites = [
/* your walking right sprite goes here*/,
/* your walking up sprite goes here*/,
/* your walking left sprite goes here*/,
/* your walking down sprite goes here*/
];
// Start facing right
facing = 0;
/// Step Event
if (keyboard_check(vk_right)) {
facing = 0;
x += move_speed;
}
if (keyboard_check(vk_up)) {
facing = 1;
y -= move_speed;
}
if (keyboard_check(vk_left)) {
facing = 2;
x -= move_speed;
}
if (keyboard_check(vk_down)) {
facing = 3;
y += move_speed;
}
// If we didn't move, use the standing sprites
// Otherwise, use the walking sprites
if (x == xprevious && y == yprevious) {
sprite_index = standing_sprites[facing];
}
else {
sprite_index = walking_sprites[facing];
}
So, just as an example
We might edit our code to something like this
Where we store all our sprites in the right spots, and we make a variable to track which way we're facing
And as we move around, we change that facing variable depending on which way we move
And we use the standing sprites or walking sprites depending on if we moved or not
So… in order to understand more about this. What does { } mean? Are they like spaces?
You can think of { } as containers
If statements run code when their condition is true, yes?
Well, how do you know what code is part of an if statement and what part isn't?
if (something)
doThis();
doThat();
doThose();
Like, if I write this
How do you tell which of these functions runs when something is true? Is doThis inside the if statement? Is doThat inside it?
There's no way to tell
if (something) {
doThis();
doThat();
}
doThose();
But if I write this instead
And wrap doThis and doThat inside { }
Now you can see that my if statement has a container under it. Anything inside the container runs when the if statement is true
Events like variables?
Not sure what you mean by that
Variables are variables, and events are events. They're pretty distinct things
Variables are the things like move_speed, it stores a bit of data. In our case move_speed stores 3 because we ran move_speed = 3;
Events don't store data, they run code when something happens. Like the Create event runs when creation happens, and the Step event runs when a game frame happens
Oh so events is what starts a code I get it
What does /* mean in the first code you showed me?
That's a comment
It's a way of saying "do something here" without writing actual code there
So I can make it really clear that you need to put your stuff there
Ah ok
So any suggestions on what should I work in first when making a project?
The player code, the object code or what?
(Or working in the sprites first)
Got it
Thanks a lot for your help mate! Do you mind if I contact you later if I need some help?
Sure, that's alright, I guess
Sorry 😓
Sorry I thought when you said “I guess” I thought you said it as if you don’t have any other option sorry 😅
Heya mate
Hello
ok so I opened gamemaker studio
I'm in a blank window
what should I add first?
I was thinking of making an RPG
(It's a Pokemon fan-game)
heya?