Game Maker's Blog: N - Type

Monday, January 02, 2006

The player

I already had a lovely bright coloured spaceship that I got ages ago (I can't remember where) which had frames of the ship tilting up and down, it's attention to the little details that players pick up on and it looks kinda neat so I used that. Then I coded in the movement of the ship in the Step Event for when the player presses the cursor keys. I used a variable called shipspeed so I could change it when the player picked up a ship-speedup powerup. Here's the code (the up / down movement is coded in seperate events at this stage):

if keyboard_check(vk_left)
{
// move the player's ship left
x -= shipspeed+1;
// Change image to normal looking spaceship
image_index = 0;
}

if keyboard_check(vk_right)
{
// move the player's ship right
x += shipspeed+1;
// Change image to normal looking spaceship
image_index = 0;
}


So now the player could move around the screen while it scrolled to the left, the only problem was when the player left go of the keys so he wasn't moving the ship, it 'moved' backwards towards the left side of the screen, and then moved out of view! Actually when the player let go of the keys, the ship wasn't moving at all! It was the room that was moving with everything in it giving the illusion that the player's ship was moving backwards on its own. I remedied this by having the ship move to the right in the no key event:

// Keep ship from moving backwards
motion_set(0,3);


This kept the player 'still' on the screen. All that was left was to keep the player from wandering outside the view on screen. As the view only really concerned the width of the view, I could check if the player wandered above or below room_height by simply putting:

// Prevent player from going off screen
y = yprevious;


in the intersect boundry event. Checking the left and right was a little trickier as the player wouldn't intersect the boundry if moving outside of the view, I also wanted the ship to be 'pushed' along if it reached the left hand side (this was left over from when I left the ship move 'backwards' if the player wasn't controlling it), so I placed this in the Step Event which did the job quite nicely:

// This will 'push' the ship along if it reaches the left-hand edge
if x-sprite_xoffset < view_xview {x = view_xview+sprite_xoffset}

0 Comments:



<< Home