• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need help with some code

H

Huganiter

Guest
Hello my name is Hunter and I'm new to GM, I took a game design class back in high school but that was a long time ago. I'm working on a game and I've been following along with this tutorial but I guess it was made before a big update came out. I'll link the YouTube video I am watching and I'll also post the code I'm having trouble with.



I put this in my PlayerAnimateSprite script:

function PlayerAnimateSprite(){

//Update Sprite
var _cardnialDirection = round(direction/90);
var _totalFrames = sprite_get_number(sprite_index) / 4;
image_index = localFrame + (_cardinalDirection * _totalFrames);
localFrame += sprite_get_speed(sprite_index) / FRAME_RATE;

//If animation would loop on next game step
if (localFrame >= _totalFrames)
{
animationEnd = true;
localFrame -= _totalFrames;
}else animationEnd = false;



}


And this is my oPlayer Step code:




// Get Player Input
keyLeft = keyboard_check(vk_left) || keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) || keyboard_check(ord("D"));
keyUp = keyboard_check(vk_up) || keyboard_check(ord("W"));
keyDown = keyboard_check(vk_down) || keyboard_check(ord("S"));
keyActivate = keyboard_check_pressed(vk_space);
keyAttack = keyboard_check_pressed(vk_shift);
keyItem = keyboard_check_pressed(vk_control);

// use (ord("A")) to use keyboard letters like "A"
// check multiple keys: keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A")); You can also use || instead of "or"


inputDirection = point_direction(0,0,keyRight-keyLeft,keyDown-keyUp);
inputMagnitude = (keyRight - keyLeft != 0) || (keyDown - keyUp != 0);

//Movement
hSpeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);

x += hSpeed;
y += vSpeed;

//Update Spite Index
var _oldSprite = sprite_index;
if (inputMagnitude != 0)
{
direction = inputDirection
sprite_index = spriteRun;
} else sprite_index = spriteIdel;
if (_oldSprite != sprite_index) localFrame = 0;

//Updating Image Index
PlayerAnimateSprite();



And this is the error I get:


ERROR in action number 1 of Step Event0 for object oPlayer: Variable oPlayer._cardinalDirection(100010, -2147483648) not set before reading it. at gml_Script_PlayerAnimateSprite (line 6) - image_index = localFrame + (_cardinalDirection * _totalFrames); ############################################################################################ gml_Script_PlayerAnimateSprite (line 6) gml_Object_oPlayer_Step_0 (line 35) - PlayerAnimateSprite();

What function do I put in the step because if I put anything for the function it'll run but I can't move my player at all and I'm not sure what to add for function ???() { }
 
Top