stolide
Level 0
Quaff, oh quaff this kind nepenthe!
|
|
« on: December 22, 2009, 02:38:15 PM » |
|
Hey everyone, this is my first question here, so forgive me if it is common. If it is, feel free to insult me, so long as you also direct me towards the answer. Think of it as your payment. How do I have the character go up an incline in game maker? I just want to be able to push the arrow key and have him walk up. I made a curvy line object following the contour of the hills in my background. He can walk down them just fine, but going up is less than agreeable. If it matters, for movement I'm giving the object a speed whilst the button is held down, and ceasing it upon release. Is there a preferable way to go about movement? Thanks in advance.
|
|
|
Logged
|
|
|
|
dbb
|
|
« Reply #1 on: December 22, 2009, 03:10:09 PM » |
|
Hey, I think the standard way to do it in GM is in every step, move the character up left or right according to player input, and at the same time move the character up (slightly more than the maximum amount a slope will rise in one step's worth of movement). Then move the character down until it it in contact with the ground (there is a drag-and-drop "move to contact" action for this). All this happens in one step, before the screen is redrawn, so from the player's perspective it'll just look like they're moving smoothly up the slope. I'm not a GM expert, though (but the above method has worked for me in my own small platformer experiments). Hopefully an actual programmer will drop by and give you a better-informed answer. feel free to insult me, so long as you also direct me towards the answer.
I hope not! Everyone was a beginner once.
|
|
|
Logged
|
|
|
|
stolide
Level 0
Quaff, oh quaff this kind nepenthe!
|
|
« Reply #2 on: December 22, 2009, 03:21:08 PM » |
|
Thanks I'll try to implement that right now. That was sort of a joke. EDIT: Another quick question, how do I add in my own variables? I try to "set" them to something (I figured it would be like LET in basic) but it tells me it does not recognize it.
|
|
« Last Edit: December 22, 2009, 03:35:22 PM by stolide »
|
Logged
|
|
|
|
Ben_Hurr
|
|
« Reply #3 on: December 22, 2009, 03:39:34 PM » |
|
You could also treat slopes as a set of tiny stairs, since that's how the computer sees it as anyway. Making your character treat a 1px high wall as a step would solve your problem.
|
|
|
Logged
|
|
|
|
dbb
|
|
« Reply #4 on: December 22, 2009, 03:45:18 PM » |
|
for movement I'm giving the object a speed whilst the button is held down, and ceasing it upon release. Is there a preferable way to go about movement?
Oh, I meant to say that your character's movement might seem more natural if you accelerate it up to max speed over a number of steps, and decelerate to zero once the player releases the key. don't overdo it, though, or your character will be drifting all over the place. The exact value of "overdoing it" in this context is a topic of endless debate among players and developers of platformers, judging by some of the threads I've read. If this is for the game with the spooky grey screenshot you posted in the "introduce yourself" thread, I'm looking forward to seeing more. Oh, Game Maker expects you to set your own variables on the fly. Like if I wanted to create a variable called "funk" and give it an initial value of zero, I'd just type: funk = 0 This is easy but a bit annoying, because it means if I later want to change the value to 1 but mistype: fumk = 1 Game Maker will just create a new variable rather than picking up the typo, but that's how it works. It will not, however, let you check a variable that doesn't exist (so if you did "if funk = 1 then..." without first declaring funk as having some value, it would break.
|
|
|
Logged
|
|
|
|
stolide
Level 0
Quaff, oh quaff this kind nepenthe!
|
|
« Reply #5 on: December 22, 2009, 03:55:02 PM » |
|
You could also treat slopes as a set of tiny stairs, since that's how the computer sees it as anyway. Making your character treat a 1px high wall as a step would solve your problem. How would I do that? That would sound like what I am trying to do. for movement I'm giving the object a speed whilst the button is held down, and ceasing it upon release. Is there a preferable way to go about movement?
Oh, I meant to say that your character's movement might seem more natural if you accelerate it up to max speed over a number of steps, and decelerate to zero once the player releases the key. don't overdo it, though, or your character will be drifting all over the place. The exact value of "overdoing it" in this context is a topic of endless debate among players and developers of platformers, judging by some of the threads I've read. If this is for the game with the spooky grey screenshot you posted in the "introduce yourself" thread, I'm looking forward to seeing more. Oh ok. How would I do acceleration and deceleration? Timers that add/subtract a relative amount to the speed while the button is held down/released? And yes. I'm going to try to make most of my mistakes on a few early games I don't care about and then move on to better things. My current plan is to make around 1 room a day for that game, each one more complicated than the previous. Oh, Game Maker expects you to set your own variables on the fly. Like if I wanted to create a variable called "funk" and give it an initial value of zero, I'd just type:
funk = 0
This is easy but a bit annoying, because it means if I later want to change the value to 1 but mistype:
fumk = 1
Game Maker will just create a new variable rather than picking up the typo, but that's how it works. It will not, however, let you check a variable that doesn't exist (so if you did "if funk = 1 then..." without first declaring funk as having some value, it would break. The bold was my problem. Also, I'm currently using the interface, rather than actually typing the code. I hope to move that direction once I can get things going relatively well with the interface. EDIT: Here's the code for how I was trying to get it to go up slopes, it isn't working. Am I going about this wrong? COMMENT: going up slopes if hspeed is larger than 0 move relative to position (2,-5) move in direction 270 at most 5 till a contact with solid objects if hspeed is smaller than 0 move relative to position (-2,-5) move in direction 270 at most 5 till a contact with solid objects
And thanks again for the help.
|
|
« Last Edit: December 22, 2009, 04:00:51 PM by stolide »
|
Logged
|
|
|
|
dbb
|
|
« Reply #6 on: December 22, 2009, 04:13:51 PM » |
|
to handle acceleration:
In the object creation event: Declare variable "maxspeed" as the maximum speed you want your character to move. Declare variable "accel" as the rate per step you want your character to accelerate. Declare variable "decel" as the rate to decelerate (probably a bit higher than "accel")
In the keyboard right event: if xspeed < maxspeed then xspeed = xspeed + accel
In the keyboard left event: if xspeed > -maxspeed then xspeed = xspeed - accel (because movement to the left is treated as a negative xspeed value)
In the keyboard (no key) event: if xspeed > 0 then xspeed = xspeed - decel if xspeed < 0 then xspeed = xspeed + decel
You can do all this with the drag-and-drop actions. xspeed is a built-in variable for all objects, so you don't have to declare it. The above is probably the easiest way to explain it.
In practice it might be better to put all the above in the step event and check manually for key presses (using keyboard_check functions (see documentation) in the "test variable" action you can still just about do this using drag-and-drop). the advantage of this is that it won't miss cases where the player is holding down neither left or right, but is holding down some other key (jump or whatever) - the example above wouldn't pick this up and the character would continue to move at a constant speed.
Also make sure that accel and decel are both factors of maxspeed, or you'll never reach zero in the keboard (no key) event.
|
|
|
Logged
|
|
|
|
Ben Kuhn
|
|
« Reply #7 on: December 22, 2009, 07:08:42 PM » |
|
This In the keyboard (no key) event: if xspeed > 0 then xspeed = xspeed - decel if xspeed < 0 then xspeed = xspeed + decel
should probably be if xspeed > 0 then xspeed = max(0, xspeed - decel) if xspeed < 0 then xspeed = min(0, xspeed + decel)
That way if xspeed is 0.25 and decel is 0.5, for example, your player won't oscillate crazily.
|
|
|
Logged
|
|
|
|
dbb
|
|
« Reply #8 on: December 23, 2009, 05:45:42 AM » |
|
This In the keyboard (no key) event: if xspeed > 0 then xspeed = xspeed - decel if xspeed < 0 then xspeed = xspeed + decel
should probably be if xspeed > 0 then xspeed = max(0, xspeed - decel) if xspeed < 0 then xspeed = min(0, xspeed + decel)
That way if xspeed is 0.25 and decel is 0.5, for example, your player won't oscillate crazily. That's really neat. I've seen GML code where someone's written a script to do a similar job and used it like if almostzero(xspeed) then xspeed = 0
but your way is much better. I'll be using this in future.
|
|
|
Logged
|
|
|
|
stolide
Level 0
Quaff, oh quaff this kind nepenthe!
|
|
« Reply #9 on: December 23, 2009, 06:10:50 AM » |
|
Thanks for the help with the acceleration, but I still cannot seem to get the character to go up an incline... Well, I got it to work once, but the character was permanently bouncing up and down.
Do you think you could post some code like how it was done for the acceleration?
|
|
|
Logged
|
|
|
|
dbb
|
|
« Reply #10 on: December 23, 2009, 07:07:37 AM » |
|
Well, regarding your "moving up slopes" code, I can't see anything that immediately jumps out at me as wrong. But, I've dug out my old half-finished platformer, and looking at it again, I realise that I don't use the built-in hspeed or vspeed variables at all. Instead I have my own "walkspeed" and "jumpspeed" variables. I can't remember exactly why I did this, but it may be that there's something with the way GM calculates hspeed behind the scenes that makes this approach not work properly - often GM tries to make things easier for you, but in doing so it just complicates matters. The way I do it is below (all in drag-and-drop, again). The following example is for moving right. text after // is comments: If walkspeed < maxspeed then walkspeed = walkspeed + accel
If relative position (walkspeed,0) is collision-free for solid objects: //flat ground jump to relative position (walkspeed,0) Else: If relative position(walkspeed, -8) is collision-free for solid objects: //sloped ground: jump to relative position (walkspeed/2,-8) //I divide walkspeed by 2 so the character walks slower uphill. //Otherwise your character will end up moving faster up slopes than along the flat, because of trigonometry and stuff. move in direction 270 at most 8 until a contact with solid objects
Answering these questions have kind of got me thinking about resurrecting this project, so thanks for that. And don't worry if it takes a while to sort out. Collision detection is hard. Even real programmers find it hard. My one bit of advice to you in general regarding this kind of stuff is: DO NOT ATTEMPT TO IMPLEMENT MOVING PLATFORMS. I tried and it nearly made me cry.
|
|
|
Logged
|
|
|
|
stolide
Level 0
Quaff, oh quaff this kind nepenthe!
|
|
« Reply #11 on: December 23, 2009, 12:03:41 PM » |
|
I'll definitely implement that tonight.
And moving platforms... You would probably just add the movement of the platform relative to the movement of the character while they are on it, right?
EDIT: I got it! That last bit worked. Thanks dbb. Something about being chased uphill by a guy with a metal shield covered in chainsaws when you cannot walk up hill (and he is to large to jump over) screams of bad gameplay to me.
|
|
« Last Edit: December 23, 2009, 01:19:09 PM by stolide »
|
Logged
|
|
|
|
Ben_Hurr
|
|
« Reply #12 on: December 23, 2009, 01:32:30 PM » |
|
...DO NOT ATTEMPT TO IMPLEMENT MOVING PLATFORMS. I tried and it nearly made me cry. I tried too, it was so hard and pointless. ;_______; ESPECIALLY WITH GM'S LACK OF CONTROL OR ORDER OF OPERATIONS.
|
|
|
Logged
|
|
|
|
FawfulBeans
Level 1
|
|
« Reply #13 on: December 23, 2009, 01:35:23 PM » |
|
...DO NOT ATTEMPT TO IMPLEMENT MOVING PLATFORMS. I tried and it nearly made me cry. I tried too, it was so hard and pointless. ;_______; ESPECIALLY WITH GM'S LACK OF CONTROL OR ORDER OF OPERATIONS. It's not that hard,dudes.
|
|
« Last Edit: December 23, 2009, 01:41:14 PM by FawfulBeans »
|
Logged
|
|
|
|
BlueSweatshirt
|
|
« Reply #14 on: December 23, 2009, 08:59:45 PM » |
|
For moving platforms: In the player->platform collision event: Check if the platform has moved(x != xprevious). If it has moved, then modify your x position according how how it's was modified.(x += other.x - other.xprevious) Simple as that! Rinse and repeat for vertically moving platforms. Thanks for the advice on the slope technique way up there dbb. Before I had this really funky code that involved a couple for loops and collision detection calls. This is much simpler and faster. Not to mention it works smoother.
|
|
|
Logged
|
|
|
|
|