Jump to content


Photo

Improving a Scrolling Shooter


  • Please log in to reply
6 replies to this topic

#1 Derme

Derme

    GMC Member

  • GMC Member
  • 277 posts
  • Version:GM:Studio

Posted 19 April 2011 - 10:49 AM

Hey, my friend just finished the scrolling shooter tutorial, and he now wants to improve it. Instead of the plane just stopping when you release any of the arrow keys, he wants it so you just slowly come to a stop. The biggest problem is that, when the plane stops, instead of having no speed, it has a vspeed of -2. I attempted to write a script that would be placed into the step event to do this.

///Slow down plane
///---Vertical Speed----////
if (vspeed<=-2)
{
vspeed+=.1
}
if (vspeed>=-2 && vspeed<=0)
{
vspeed-=.1
}
if (vspeed>=0)
{
vspeed-=.1
}
else if (vspeed=-2)
{
vspeed=-2;
}

///---Horizontal Speed---////
if (hspeed>=0)
{
hspeed-=.1
}
if (hspeed<=0)
{
hspeed+=.1
}
else if (hspeed=0)
{
hspeed=0;
}


I also changed the arrow keys so that instead of jumping to a position, instead they would just increase the vspeed/hspeed by 5 in the desired direction.

e.g. Right Arrow Key

if (x<=room_width-80)
hspeed=5

The problem/s- Firstly, I think the slowing down script could be written better, so any help with that would be great. But the real problem is that when you release the right or up arrow keys, the plane slows to an almost stop and keeps moving right/up. All the other keys work as they should, though.

Here is an attached example of the problem.

Download Mirror 1

(NOTE: This is not his actually game, just the GM8 tutorial with the code added)

Thanks in advance,
-Derme

Edited by Derme, 20 April 2011 - 12:21 AM.

  • 0

#2 Davido01

Davido01

    GMC Member

  • New Member
  • 905 posts

Posted 19 April 2011 - 02:00 PM

if speed > 0
    motion_add(direction,-0.2)

Try that in the brake event. If its glitchy I have a fix.
  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16791 posts
  • Version:GM:Studio

Posted 19 April 2011 - 03:01 PM

Instead of all that code you have in the step event, just use this...

if !keyboard_check(vk_left) and !keyboard_check(vk_right) and !keyboard_check(vk_up) and !keyboard_check(vk_down)
{
friction=0.1; //Change 0.1 for different values for different effects...
}
else
{
if abs(speed)>20 speed=20*sign(speed); //Limit the movement maximum speed to 20... change as needed!
friction=0;
}

Okay the above code checks to see if NONE ("!" means "not") of the movement keys have been pressed, and if that is the case, it applies friction to slow down the object. If any of those keys are pressed, it removes friction and also checks the speed to make sure that you don´t go over the maximum speed limit... this last part is optional, but is handy to prevent your plane moving at 100px per step if you hold the key down too long!

Hope that helps!

PS: If you want acceleration too, then use "hspeed+=1" (right) in the key events (or "hspeed-=1" for left) to give a more gradual movement.

Edited by Nocturne, 19 April 2011 - 03:02 PM.

  • 1

#4 Derme

Derme

    GMC Member

  • GMC Member
  • 277 posts
  • Version:GM:Studio

Posted 20 April 2011 - 12:01 AM

if speed > 0
    motion_add(direction,-0.2)

Try that in the brake event. If its glitchy I have a fix.


I did try it, and it was a bit glitchy. ;) It didn't stop the plane perfectly on the down and right arrow key events, it had the same problem as my script where the plane would continue in that direction at a speed of about .2 and I need the plane to always have a vspeed of -2.


Instead of all that code you have in the step event, just use this...

if !keyboard_check(vk_left) and !keyboard_check(vk_right) and !keyboard_check(vk_up) and !keyboard_check(vk_down)
{
friction=0.1; //Change 0.1 for different values for different effects...
}
else
{
if abs(speed)>20 speed=20*sign(speed); //Limit the movement maximum speed to 20... change as needed!
friction=0;
}

Okay the above code checks to see if NONE ("!" means "not") of the movement keys have been pressed, and if that is the case, it applies friction to slow down the object. If any of those keys are pressed, it removes friction and also checks the speed to make sure that you donīt go over the maximum speed limit... this last part is optional, but is handy to prevent your plane moving at 100px per step if you hold the key down too long!

Hope that helps!

PS: If you want acceleration too, then use "hspeed+=1" (right) in the key events (or "hspeed-=1" for left) to give a more gradual movement.


Alright this script slowed the plane down perfectly, except it completely stopped the plane, instead of just slowing down to a vspeed of -2. Is there anyway that this could be added in, without effecting the rest of the script. Oh and acceleration is a great idea by the way.

Thanks,
Derme
  • 0

#5 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16791 posts
  • Version:GM:Studio

Posted 20 April 2011 - 12:14 AM

Sure that can be modified... but we need to do things a bit differently!

//CREATE EVENT
fric=0.1; //Custom friction variable...

if !keyboard_check(vk_up) and !keyboard_check(vk_down)
{
if vspeed>-2  vspeed-=fric else if vspeed<-2 vspeed+=fric;
}

if !keyboard_check(vk_left) and !keyboard_check(vk_right)
 {
if abs(hspeed)>0  hspeed-=fric*sign(hspeed);
 }

As you can see, we have split the two sets of horizontal and vertical keypresses into two groups... this is because the vertical bottoms out at -2 speed but the horizontal at 0, so wee need to treat them differently. As for the limiting code that I placd last time, I would move it into the respective key events, so they will look something like this...

//KEYBOARD ARROW RIGHT
hspeed+=1;
if hspeed>20 hspeed=20;

//KEYBOARD ARROW LEFT
hspeed-=1;
 if hspeed<-20 hspeed=-20;

And the same for the vertical speed too.

Hope that helps!
  • 1

#6 Derme

Derme

    GMC Member

  • GMC Member
  • 277 posts
  • Version:GM:Studio

Posted 20 April 2011 - 12:25 AM

Sure that can be modified... but we need to do things a bit differently!

//CREATE EVENT
fric=0.1; //Custom friction variable...

if !keyboard_check(vk_up) and !keyboard_check(vk_down)
{
if vspeed>-2  vspeed-=fric else if vspeed<-2 vspeed+=fric;
}

if !keyboard_check(vk_left) and !keyboard_check(vk_right)
 {
if abs(hspeed)>0  hspeed-=fric*sign(hspeed);
 }

As you can see, we have split the two sets of horizontal and vertical keypresses into two groups... this is because the vertical bottoms out at -2 speed but the horizontal at 0, so wee need to treat them differently. As for the limiting code that I placd last time, I would move it into the respective key events, so they will look something like this...

//KEYBOARD ARROW RIGHT
hspeed+=1;
if hspeed>20 hspeed=20;

//KEYBOARD ARROW LEFT
hspeed-=1;
 if hspeed<-20 hspeed=-20;

And the same for the vertical speed too.

Hope that helps!


*SOLVED*

Thank You ;) , It works perfectly, not a single problem.

-Derme
  • 0

#7 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16791 posts
  • Version:GM:Studio

Posted 20 April 2011 - 12:27 AM

Glad to hear it! All the best!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users