#Need some help with a code.

91 messages · Page 1 of 1 (latest)

worn cradle
#

Okay then. Do you just want to move up when pressing up, and so on? No animations, no acceleration, no grid based movement, nothing but just changing x and y?

#

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

worn cradle
#

Pardon?

fathom jewel
worn cradle
#
/// 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

fathom jewel
#

Hmm noted…

worn cradle
#

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

fathom jewel
#

And where should I type the speed?

worn cradle
#

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

fathom jewel
#

Got it

worn cradle
#

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

fathom jewel
#

What if I want it to add the other stuff you said like animation, acceleration etc

worn cradle
#

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

fathom jewel
#

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

worn cradle
#

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

fathom jewel
#

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?

worn cradle
#
/// 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

fathom jewel
#

Got it…

#

Now comes the hard part to understand all of this 😅

fathom jewel
worn cradle
#

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

fathom jewel
#

Now that makes sense

#

Are the events like the variables?

worn cradle
#

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

fathom jewel
#

Oh so events is what starts a code I get it

fathom jewel
worn cradle
#

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

fathom jewel
#

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)

worn cradle
#

That's kinda up to you

#

You'll have to do all that stuff eventually

fathom jewel
#

Got it

fathom jewel
worn cradle
#

Sure, that's alright, I guess

fathom jewel
worn cradle
#

Hm?

#

What's wrong?

fathom jewel
# worn cradle What's wrong?

Sorry I thought when you said “I guess” I thought you said it as if you don’t have any other option sorry 😅

fathom jewel
worn cradle
fathom jewel
#

I'm in a blank window

#

what should I add first?

worn cradle
#

Wha?

#

Uh, doesn’t that depend on what you want to make?

fathom jewel
#

(It's a Pokemon fan-game)

worn cradle
#

Okay

#

Welp, I wish you luck! Hope it goes well!

fathom jewel