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

Pages: [1]   Go Down

Author Topic: [Request / Listing] Really simple code help...  (Read 1827 times)

0 Members and 1 Guest are viewing this topic.
[Request / Listing] Really simple code help...
« on: May 23, 2006, 07:04:44 pm »
  • odens knop
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1608
...well, I'm starting to learn GML, and I'm making a walking engine as my first programmed GM application.  I've done D&D before but I didn't like it at all, so now I'm giving programming a try.  So I'm wondering what's wrong with this code:

Code: [Select]
if keyboard_check_released(vk_anykey)
image_index= 0
image_speed= 0
if keyboard_check_pressed(vk_down)
image_speed= 0.5

What I want to do is have the sprite to have the first image of the animated sprite when no buttons are pressed, and play the animation when the down button is pressed.

[Edit]

Code: [Select]
pressd= keyboard_check(vk_down)
pressu= keyboard_check(vk_up)
pressl= keyboard_check(vk_left)
pressr= keyboard_check(vk_right)
releaseany= keyboard_check_released(vk_anykey);
{
if pressd= true
image_index= 0
image_speed= 0
}
if releaseany= true
image_speed= 0.5

^^Didn't seem to help... :(
« Last Edit: March 06, 2012, 09:36:10 am by Niek »
Logged
|LEUS HERTAN MINAT|
Re: Really simple code help...
« Reply #1 on: May 23, 2006, 08:03:37 pm »
  • =/
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2284
Ok try this:

if keyboard_check(vk_anykey) = false //If no key is pressed
image_index= 0
image_speed= 0
if keyboard_check(vk_down) = true //If down is pressed
image_speed= 0.5

You shouldn't use released and pressed and instead you should use true and false. You could also use:

if keyboard_check(vk_nokey) //If no key is pressed
image_index= 0
image_speed= 0
if keyboard_check(vk_down) = true //If down is pressed
image_speed= 0.5

« Last Edit: May 23, 2006, 08:06:22 pm by piers »
Logged
Re: Really simple code help...
« Reply #2 on: May 23, 2006, 08:11:49 pm »
  • odens knop
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1608
Thanx!  :)  I'll try it tomorrow.  I'll edit my post.
Logged
|LEUS HERTAN MINAT|

Goodnight

Once and future Captain
Re: Really simple code help...
« Reply #3 on: May 23, 2006, 08:36:22 pm »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
That suggestion has a flaw: If you start moving with the arrow keys, then hold any other key (such as Z) and release the arrow, the sprite will keep animating. You only want to stop the animation if none of the arrow keys are being held.

You could do that with either:
if pressd=false && pressu=false && pressl=false && pressr=false

Or, as I would prefer:
Code: [Select]
if pressd=true {
image_speed=0.5
// code here to walk down
} else if pressu=true {
image_speed=0.5
// code here to walk up
} else if pressl=true {
image_speed=0.5
// code here to walk left
} else if pressr=true {
image_speed=0.5
// code here to walk right
} else {
image_index= 0
image_speed= 0
}
What that does is check through the four keys in order, only checking each condition if the one before it was false. So if you aren't holding down, up, left, or right, then do the stopping code. NOTE: That example is very basic and doesn't account for diagonal movement, so you may want to go with the first suggestion instead.

Also, please read my post here for some important info on how to use curly brackets with conditions. :)
Logged
Re: Really simple code help...
« Reply #4 on: May 24, 2006, 01:29:31 am »
  • =/
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2284
That suggestion has a flaw: If you start moving with the arrow keys, then hold any other key (such as Z) and release the arrow, the sprite will keep animating. You only want to stop the animation if none of the arrow keys are being held.

You could do that with either:
if pressd=false && pressu=false && pressl=false && pressr=false
I was going to show that but he said he is just learning gml so its not really that important. You could also have a variable instead of writing image_speed hundreds of times like this:

if keyboard_check(vk_down){
 //walking code and !@#$%
 global.position = 'Walking' //It could also be 0 and 1 but I like doing crap like this
}

if !keyboard_check(vk_down) and !keyboard_check(vk_up) and !keyboard_check(vk_right) and !keyboard_check(vk_left){
 global.position = 'Nothing'
}

if global.position = 'Walking'{
 image_speed = 0.4
}

if global.position = 'Nothing'{
 image_speed = 0
}

That also works but its not exactly optimized but it works for me. 
Logged

Goodnight

Once and future Captain
Re: Really simple code help...
« Reply #5 on: May 24, 2006, 03:02:10 am »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
That's fine, jus' helping out. ;)

While we're on the subject of learning GML, here's another tip for Linky:

if keyboard_check(vk_down)
means the same as:
if keyboard_check(vk_down)=true

and...

if !keyboard_check(vk_down)
means the same as:
if keyboard_check(vk_down)=false
Logged
Re: Really simple code help...
« Reply #6 on: May 24, 2006, 05:01:45 am »
  • odens knop
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1608
Thanks for all the help you guys, I'll ask you if I have any questions.  :)
I ended up using:

Code: [Select]
pressd= keyboard_check(vk_down)
pressu= keyboard_check(vk_up)
pressl= keyboard_check(vk_left)
pressr= keyboard_check(vk_right)
{
if pressd=false && pressu=false && pressl=false && pressr=false
image_index= 1
image_speed= 0
}
if pressd=true
image_speed= 0.5

And before it worked properly I notised another flaw, but it was just me though, I didn't have the sprite set up right, but I fixed it.  And now I'm gonna try to make him actually move. :P
Logged
|LEUS HERTAN MINAT|
Pages: [1]   Go Up

 


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



Page created in 0.286 seconds with 51 queries.