Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1] 2   Go Down

Author Topic: Platforming Game Movement Trouble  (Read 4817 times)

0 Members and 1 Guest are viewing this topic.

Ryuza

That one guy
Platforming Game Movement Trouble
« on: September 25, 2010, 09:51:20 pm »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Been having a bit of trouble with getting some sidescrolling basic movement, messing around with a new way of doing it using while statements:

Code: [Select]
// Step Event
if keyboard_check(vk_left) {
    dir = "L"
    while hspd != -2 {hspd -= .02}
} else if !keyboard_check(vk_left) {
    while hspd != 0 {hspd += .02}
}

if keyboard_check(vk_right) {
    dir = "R"
    while hspd != 2 {hspd += .02}
} else if !keyboard_check(vk_right) {
    while hspd != 0 {hspd -= .02}
}

Whats happening is that the game freezes up the second I press left or right, if I take out the code for the left key it works fine and vice versa, so they're interfering with each other somehow. I'm not quite sure whats going on with it and I'm still looking it over and working on it but I figured its always easier to find a solution from the outside and since I'm tangled up in it I can't seem to figure it out, no matter how obvious its likely to be.

So, any help is quite appreciated. Thanks in advance.
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Re: Platforming Game Movement Trouble
« Reply #1 on: September 25, 2010, 10:27:08 pm »
  • d(-_-)b
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 36
When you are pressing left your speed goes to -2, then when you are not pressing right the while statement enter in a infinite loop that freezes the game.
Logged

Ryuza

That one guy
Re: Platforming Game Movement Trouble
« Reply #2 on: September 25, 2010, 10:37:15 pm »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
...Wow, how'd I miss that. Thanks a lot, fixed it pretty quick, just changed
Code: [Select]
while hspd != -2 to
Code: [Select]
while hspd != -2 && hspd <= 0 and vice versa for the right key, thanks again.
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Re: Platforming Game Movement Trouble
« Reply #3 on: September 25, 2010, 11:38:38 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
Your original code has some things which would need to be corrected or can be left out due to them not being necessary. I edited into your code stuff:
Code: [Select]
// Step Event
if keyboard_check(vk_left) {
    dir = "L"
    while hspd != -2 {hspd -= .02}
   //these while loops won't show incremental movement because the speed
   //if the code is done correctly will go to its maximum each step; i.e., it will
   //loop until the speed is at its upper and lower bounds.
} else if !keyboard_check(vk_left) {
  //you don't need the above !keyboard_check(vk_left) because you already
  //checked the keyboard_check(vk_left). If your code evaluated keyboard_check(vk_left)
  //and moved past it logically because it wasn't true, then it must therefore be false
    while hspd != 0 {hspd += .02}
}

if keyboard_check(vk_right) {
    //also for this stuff just use the built in direction variable, with this you are probably
   //just wasting memory space
    dir = "R"
    while hspd != 2 {hspd += .02}
} else if !keyboard_check(vk_right) {
    while hspd != 0 {hspd -= .02}
}


I am not sure exactly what you are trying to go for with your platforming game but I souped up the code a little bit. The code concept is a take off of the ice movement stuff I once coded into the GM Minish Cap Engine and would offer you the ability to customize values so that you can tweak the movement ability of your object.

Create Event:
Code: [Select]
step_x = 0;
acce_x = 0.4;
dece_x = 0.2;
move_x = 0;
maxi_x = 5;

Step Event:
Code: [Select]
var hold_l,hold_r,absv_x;

hold_l = keyboard_check(vk_left);
hold_r = keyboard_check(vk_right);

if (hold_l ^^ hold_r){
  direction = hold_l * 180;
  move_x += (hold_r - hold_l) * acce_x;;
  if (move_x < -maxi_x)
    move_x = -maxi_x;
  else if (move_x > maxi_x)
    move_x = maxi_x;
}
else if (move_x < 0)
  move_x = min(move_x + dece_x,0);
else if (move_x > 0)
  move_x = max(move_x - dece_x,0);
else
  move_x = 0;

absv_x = abs(move_x);
if (absv_x){
  if (absv_x == maxi_x)
    step_x = 0;
  step_x += absv_x;
  while (step_x >= 1){
    x += (move_x > 0) - (move_x < 0);
    step_x -= 1;
  }
}


With this code you can build up and slow down speed appropriately; you can alter the values in the create event so that you can either speed up or slow down faster or slower, or have a higher or lower top speed.

Logged

Ryuza

That one guy
Re: Platforming Game Movement Trouble
« Reply #4 on: September 26, 2010, 02:24:51 am »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Very nice, you kinda just blew my stuff out of the water, lol. I think I'll try adding jumping and gravity to the code you posted, thanks 4Sword.

Quick question though; What's ^^ do? I know about stuff like || and && but I haven't seen that one before.
« Last Edit: September 26, 2010, 02:26:44 am by Ryuza »
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Re: Platforming Game Movement Trouble
« Reply #5 on: September 26, 2010, 02:29:52 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I was also testing some stuff out with sprites from Super Mario World and found that since I don't have to worry about drawing a shadow, I can just set xscale to -1 when I want to draw the image reversed. I can cut the amount of sprites in half for stuff like that and reduce the overall amount of decision statements, it is kind of fancy. Also, the "^^" symbols mean xor where either the left side or right side of the logic statement is true then the statement is true, otherwise if both are true or both are false then the statement is false.
Logged

Ryuza

That one guy
Re: Platforming Game Movement Trouble
« Reply #6 on: September 26, 2010, 02:36:23 am »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Ah, I had been wondering about something like that to flip the sprites and cut down a bit on size, I had tried a different way of flipping them before but it didn't work too well.

Knowing about ^^ would have helped me so much before... Well thanks for letting me know about that though.

Also, if its not too much trouble do you think I could get that movement code you just posted with a few comments? I didn't want to just take it and use it, I was hoping I could look it over and figure out how you did everything and then go back to rework my code (a lot, lol).
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Re: Platforming Game Movement Trouble
« Reply #7 on: September 26, 2010, 03:10:22 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I could post the movement code with comments on how it works but it isn't like it is fully complete because it doesn't test for collisions; basically you'd use the collision_rectangle function to check the distance you are able to move unobstructed at the maximum speed. When this is used the code actually gets a small bit of revision so that when it detects nothing, instead of doing addition checks to see what specific objects that are being run into, it will just do the movement. Also the sprite flipping stuff I mentioned works but I just figured out that I have to set the x of origins of the sprites used to be about the middle of the sprite.

Anyway though he is the code for what I have posted so far with comments:
Create Event:
Code: [Select]
step_x = 0; //used to later determine how much character moves per step
acce_x = 0.4; //rate at which the character accelerates
dece_x = 0.2; //rate at which the character decelerates
move_x = 0; //the speed at which the character moves
maxi_x = 5; //the maximum speed at which the character can move

Step Event:
Code: [Select]
var hold_l,hold_r,absv_x;

//store the held values for the left and right keys
hold_l = keyboard_check(vk_left);
hold_r = keyboard_check(vk_right);

//if either only left or only right is pressed
if (hold_l ^^ hold_r){
  //since direction 0 represents "right" the code below will be 0 if
  //the left key is held, 180  represents "left"
  direction = hold_l * 180;

  //accelerate or decelerate a little more either left or right
  move_x += (hold_r - hold_l) * acce_x;

  //if the speed is less than its most negative allowed value
  //or is greater than its most positive allowed value, then
  //set the speed to be at its maximum respectively
  if (move_x < -maxi_x)
    move_x = -maxi_x;
  else if (move_x > maxi_x)
    move_x = maxi_x;
}
else if (move_x < 0) //if not holding left/right, slow down if moving left
  move_x = min(move_x + dece_x,0);
else if (move_x > 0) //if not holding left/right, slow down if moving right
  move_x = max(move_x - dece_x,0);
else //if not moving at all set it to be so you're not moving - this might actually not be necessary, lol, I don't know
  move_x = 0;

//store the absolute value of the move speed so that you can work with it
absv_x = abs(move_x);

//if you have a move speed, the lowest value absv_x can be is 0 which represents no movement
if (absv_x){
  //if you are at the max speed, clear out step_x so that it updates freshly
  if (absv_x == maxi_x)
    step_x = 0;

  //add the movement speed to the number of times you move each step
  step_x += absv_x;

  //if your step_x is something like 2.4, you only move 2 times
  while (step_x >= 1){
    //you don't always move in the direction of your left/right key holding
    //this handles the "drifting" and keeps it smooth
    x += (move_x > 0) - (move_x < 0);

    //this is necessary to get you out of the loop
    step_x -= 1;
  }
}
Logged

Ryuza

That one guy
Re: Platforming Game Movement Trouble
« Reply #8 on: September 26, 2010, 03:16:31 am »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Thanks a lot 4Sword, this should really help me with my programming. I'm gonna go over everything now and see what I can learn from it so I'll post back if I run into any trouble in the future.

Thanks again.
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Re: Platforming Game Movement Trouble
« Reply #9 on: September 26, 2010, 05:24:26 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
Also if anyone was wondering what this looks like in action with Mario sprites I put something together - it still needs tweaking but meh.


The download link is here: http://www.zfgc.com/index.php#?action=resources&sa=view&id=205
Logged
Re: Platforming Game Movement Trouble
« Reply #10 on: September 26, 2010, 05:27:47 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
the fact that it is GM8 would have been nice to know :p lol

EDIT: WTF? how can you guys code like that? jeeze
So much easier to not make a mistake using
Code: [Select]
if <expression>
{
       if <expression>
       {
               <statement>;
       }
}
else
{
       <statement>;
}

This just screams "make mistakes" to me:
Code: [Select]
if <expression>{
  <statement>;}
else if <expression>
        <statement>;
 
I guess it is how you are taught but, damn...So many engines do that crap.  It doesn't look uniform at all.
Spaces help keep me focused so that if i do walk away from something then come back, I know exactly what's going on without the need for //comments in my code.  Plus I can easily find errors with white space to space everything out.
I forgot the word that describes the way I "code"...yours has a word too.  Damnit, now that is going to bug me.
Part of the main reason I never helped with ZFGC Minish Engine Community Project, the way the code is written :/
Also, every book/tutorial website/article I personally have read/own has the code the way I do it.
Just saying.
« Last Edit: September 26, 2010, 05:37:13 am by Theforeshadower »
Logged
  • Super Fan Gamers!

Mamoruanime

@Mamoruanime
Re: Platforming Game Movement Trouble
« Reply #11 on: September 26, 2010, 05:36:22 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
You can totally know by the gear in the image he posted ;p
Logged
Re: Platforming Game Movement Trouble
« Reply #12 on: September 26, 2010, 05:38:53 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
You can totally know by the gear in the image he posted ;p
well...erm....*cough*....
Logged
  • Super Fan Gamers!
Re: Platforming Game Movement Trouble
« Reply #13 on: September 26, 2010, 05:46:15 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
Sorry about that; I altered the download file it should now include a GM7 and a GM8 file. Also about coding indenting stuff, when I took a Java course I coded more like you do except that I would do something like:

Code: [Select]
if (expression)
  {
  statement;
  }

Otherwise though the style I use now is what Niek's style was more like so that is why I went to it. It makes the code feel shorter and if you code enough in the style it's logical progression is easy enough to follow.
« Last Edit: September 26, 2010, 05:49:58 am by 4Sword »
Logged

Mirby

Drifter
Re: Platforming Game Movement Trouble
« Reply #14 on: September 26, 2010, 06:26:04 am »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
Umm... can I use this please? I've been stuck with the same issues as Ryuza here.
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios
Re: Platforming Game Movement Trouble
« Reply #15 on: September 26, 2010, 06:32:14 am »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
Anyone can use it, if used just give ZFGC credit. It isn't complete platform stuff though, like I said earlier there is no collision system with that yet. It wouldn't be like a Zelda-type collision system either - that runs on the idea that Link is running in an open space and then runs into things. Mario would have to either have the gravity turned off when he wasn't jumping or falling off of stuff or would be constantly falling into stuff. Like, the collision systems would be different in Link's being mostly free where as Mario's would be mostly not.
Logged
Re: Platforming Game Movement Trouble
« Reply #16 on: September 26, 2010, 06:59:01 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 41
Sorry about that; I altered the download file it should now include a GM7 and a GM8 file. Also about coding indenting stuff, when I took a Java course I coded more like you do except that I would do something like:

Code: [Select]
if (expression)
  {
  statement;
  }

Otherwise though the style I use now is what Niek's style was more like so that is why I went to it. It makes the code feel shorter and if you code enough in the style it's logical progression is easy enough to follow.
I used to do that but for the sake of making files as light as possible I would use as little spaces or line breaks as I could. I know it's not much thinking 1-bit here, 1 there but they add up over a few thousand lines and a dozen or so files.

Code: [Select]
if(expression){statement;}
Logged

Mamoruanime

@Mamoruanime
Re: Platforming Game Movement Trouble
« Reply #17 on: September 26, 2010, 07:02:54 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Sorry about that; I altered the download file it should now include a GM7 and a GM8 file. Also about coding indenting stuff, when I took a Java course I coded more like you do except that I would do something like:

Code: [Select]
if (expression)
  {
  statement;
  }

Otherwise though the style I use now is what Niek's style was more like so that is why I went to it. It makes the code feel shorter and if you code enough in the style it's logical progression is easy enough to follow.
I used to do that but for the sake of making files as light as possible I would use as little spaces or line breaks as I could. I know it's not much thinking 1-bit here, 1 there but they add up over a few thousand lines and a dozen or so files.

Code: [Select]
if(expression){statement;}

I was about to say that "whitespace is removed from code, so it shouldn't affect filesize", but sometimes I forget that GM doesn't actually compile it's code. It just lets it sit in memory :P
Logged
Re: Platforming Game Movement Trouble
« Reply #18 on: September 26, 2010, 07:21:01 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
the fact that it is GM8 would have been nice to know :p lol

EDIT: WTF? how can you guys code like that? jeeze
So much easier to not make a mistake using
Code: [Select]
if <expression>
{
       if <expression>
       {
               <statement>;
       }
}
else
{
       <statement>;
}

This just screams "make mistakes" to me:
Code: [Select]
if <expression>{
  <statement>;}
else if <expression>
        <statement>;
  
I guess it is how you are taught but, damn...So many engines do that crap.  It doesn't look uniform at all.
Spaces help keep me focused so that if i do walk away from something then come back, I know exactly what's going on without the need for //comments in my code.  Plus I can easily find errors with white space to space everything out.
I forgot the word that describes the way I "code"...yours has a word too.  Damnit, now that is going to bug me.
Part of the main reason I never helped with ZFGC Minish Engine Community Project, the way the code is written :/
Also, every book/tutorial website/article I personally have read/own has the code the way I do it.
Just saying.

Actually the last style of coding is a well accepted way of coding and most books that teach you a programming language. Both your examples are essentially the same. Both are nested selection structures. In most coding languages you have several selection structures. In C(family), Java and GML you have 3 types.
  • if - selection
  • if...else - selection
  • switch - selection
And in many languages these control structures (sequence, repetition, selection) can be nested. And each structure counts as a single statement. Take for example the following nested structures as it contains 2 of the selection structures mentioned.

Code: [Select]
if <expression1>
    <statement1>
else
    if <expression2>
        <statement2>
    else
        if <expression3>
            <statement3>

For compilers indentation does not matter at all and when you have about four or five of these nested structures it becomes more difficult to read. Having about 10 to 20 the indentation probably sends it of the side of the screen (not that that will happen much). Thus for better readability many programmers and instruction books will write the above code as:

Code: [Select]
if <expression1>
    <statement1>
else if <expression2> //the fiirst nested selection
    <statement2>
else if <expression3> //the second nested selection
    <statement3>

This will keep the indentation lower and still have more readability. You can still add white lines to it if you wish. Now about the '{' and '}'. As you can see a control structure only accepts 1 statement and a statement being the like of c = a + b;. In languages like Java and C(family) a statement ends with ';', but in GML it is not required. However when you want to have multiple statements you use '{' and '}'. This results in that the entire block of statements is considered as 1 statement.

When just having 1 statement it is still correct to use '{' and '}'. Some programmers consider it propper etiquette to always use them, some don't and prefer not to use them for a single statement. Me, I don't care.



I was about to say that "whitespace is removed from code, so it shouldn't affect filesize", but sometimes I forget that GM doesn't actually compile it's code. It just lets it sit in memory :P
Besides that, it is often a readability issue. Try having a dozen nested structures indented and some people leave the indentation of 1 tab equal to 8 spaces.
« Last Edit: September 26, 2010, 07:24:08 am by Niek »
Logged
Re: Platforming Game Movement Trouble
« Reply #19 on: October 08, 2010, 06:56:13 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Tried out this code in GM8. 
Messed with it a bit by adding some simple collisions.  Came to a spot where the collision works nicely, but the animation is changing per step back and fourth from standing to walking.
I know where it is at:
Code: [Select]
if (!absv_x)
{
  if (hold_u ^^ hold_d)
  {
    if (hold_u)
      image_index = 1;
    else
      image_index = 2;
  }
  else
  {
    image_index = 0;
  }
  if (jumping = 0 && falling = 0)
  {
  sprite_index = spr_sm1standright;
  image_speed = 0;
  }
}

Yes, I indented it.  Was annoying the !@#$% out of me.

I just added a new condition and changed the while to and if here:
Code: [Select]
if (absv_x)
{
  if (absv_x == maxi_x)
    step_x = 0;
  step_x += absv_x;
  if (step_x >= 1) && (!place_meeting(x + move_x,y,obj_safesolid))
  {
   
    x += (move_x > 0) - (move_x < 0);
    step_x -= 1;
  }
  else if (place_meeting(x + move_x,y,obj_safesolid))
  {
    move_x = 0;
  }
 
}

It doesn't go as fast as how you had it 4sword, but I like the result.
However, since it is setting move_x to 0, it resets my animation.  So, I now I need to stop movement of the player without changing the animation.  I personally liked the walking animations to keep playing when the player was walking against walls like in the old NES days.

Should probably mention that I am using a mask for collisions.


EDIT: Nevermind, solved the problem like I usually do after I post here.  Now, I get to work with jumping and falling :D
EDIT 2: Jumping and falling working nicely >.> I should post more things on ZFGC so I can solve them faster.
« Last Edit: October 08, 2010, 07:11:47 am by Theforeshadower »
Logged
  • Super Fan Gamers!
Pages: [1] 2   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.052 seconds with 75 queries.

anything