Computer Science Canada moving my craracter |
Author: | metachief [ Wed Feb 06, 2008 4:53 pm ] |
Post subject: | moving my craracter |
Hey, fellow programmers, I have a small problem with my code. I can make my character move one direction, but then when i press another key to make it go the other way it doesn't work. In other words: which direction i pick from the start i can not change unless i quit turing and launch it again. So I was wondering if any of you could suggest something about the lay out of my code, or tell me if anything i wrote is wrong. TURING CODE: [color=#00ff11][color=#00ff11][color=#00ff11]var pics : array 1 .. 10 of int var sprite : int var move_key : array char of boolean var chars : array char of boolean var x, y : int := 100 proc up % not needed end up proc down % not needed end down proc left loop for i : 1 .. 10 pics (i) := Pic.FileNew ("pic" + intstr (i) + "_l.bmp") sprite := Sprite.New (pics (i)) Sprite.SetPosition (sprite, x, y, true) Sprite.Show (sprite) loop Input.KeyDown (chars) if chars (KEY_LEFT_ARROW) then x -= 10 exit end if end loop for k : 8 .. maxx by 100 Sprite.ChangePic (sprite, pics (i)) delay (11) end for Sprite.Free (sprite) end for end loop end left proc right loop for i : 1 .. 10 pics (i) := Pic.FileNew ("pic" + intstr (i) + "_r.bmp") sprite := Sprite.New (pics (i)) Sprite.SetPosition (sprite, x, y, true) Sprite.Show (sprite) loop Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then x += 10 exit end if end loop for k : 8 .. maxx by 100 Sprite.ChangePic (sprite, pics (i)) delay (11) end for Sprite.Free (sprite) end for end loop end right loop colorback (7) Input.KeyDown (move_key) if move_key (KEY_UP_ARROW) then % not needed up elsif move_key (KEY_DOWN_ARROW) then % not needed down elsif move_key (KEY_LEFT_ARROW) then left elsif move_key (KEY_RIGHT_ARROW) then right end if end loop |
Author: | metachief [ Wed Feb 06, 2008 4:54 pm ] |
Post subject: | Re: moving my craracter |
by the way the color stuff at the begining of my code is irrelavant..mistake so get rid of it if you test |
Author: | MichaelM [ Wed Feb 06, 2008 5:49 pm ] |
Post subject: | Re: moving my craracter |
Well its hard to test what you've got by just what you've posted here. Could you post the sprites and any other code you have to go along with it? Also, if you havent seen them already, here are a few tutorials that might help you with your problem: How to Move an Object Properly - http://compsci.ca/v3/viewtopic.php?t=7587&highlight=movement Movemeny (getch,keydown,mouse) - http://compsci.ca/v3/viewtopic.php?t=3275&highlight=movement Sprites - http://compsci.ca/v3/viewtopic.php?t=4598&highlight=movement Also, when posting code could you use the code tags, they makes things easier to read/look at ![]() |
Author: | BigBear [ Wed Feb 06, 2008 5:51 pm ] | ||
Post subject: | Re: moving my craracter | ||
Next time use the BBCode link on the side when posting code
And I can't and do not know alot about using Sprits. But if you run say procedure left your get input again y not just use the first oneif user presses KEY_LEFT_ARROW then move the image. |
Author: | Sean [ Wed Feb 06, 2008 6:51 pm ] |
Post subject: | Re: moving my craracter |
Suggestion, the use of for loops shortens your code, and gets the exact same effect you have coded. Unless, you want Key Movement, I suggest making a x and y variable for your picture. When a key is pressed then move it accordingly to the direction. |
Author: | Carey [ Fri Feb 08, 2008 12:00 pm ] |
Post subject: | RE:moving my craracter |
Ahh my eyes!! Use code tags. The continuous movement problem is very simple. you have an indefinite loop. you exit out of the fist one, but not the second one. This means that you can only go one direction because the proc left or right never ends! |