Computer Science Canada Making my character move left and right using sprites |
Author: | dragonboy456 [ Mon Apr 06, 2015 10:40 pm ] |
Post subject: | Making my character move left and right using sprites |
What is it you are trying to achieve? I am trying to animate my hulk so that it moves left and right using sprites and not just on one spot. I know how to create a new sprite but I dont know how to insert more than one picture for the sprite. I don't know if i am making this clear. I also have to make it so that if the user presses the right arrow key than it moves right and if he or she presses the left arrow key, than the hulk will move left. When it moves, the picture should animate and not just be the same picture. This is what I have so far. It's not a lot but I'm trying to understand animation better. When I press the right key, it does't move at all. Any help would be greatly appreciated Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) var chars : array char of boolean var NewSprite, pics, standR, standL, standRightID, standLeftID : int var x, y : int := 100 standRightID := Pic.FileNew ("lookRight.bmp") standR := Sprite.New (standRightID) standLeftID := Pic.FileNew ("lookLeft.bmp") standL := Sprite.New (standLeftID) Sprite.SetHeight (standR, 1) Sprite.SetPosition (standR, x, y, false) Sprite.Show (standR) loop Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then %Sprite.Hide (standR) Sprite.Hide (standL) x:= x + 5 %pics := Pic.FileNew ("right1.bmp") %NewSprite := Sprite.New (pics) %Sprite.ChangePic(NewSprite, "right1.bmp") %Sprite.Animate (NewSprite, end if if chars (KEY_DOWN_ARROW) then Sprite.Hide (standR) Sprite.Hide (standL) end if if chars (KEY_UP_ARROW) then Sprite.Hide (standR) Sprite.Hide (standL) end if if chars (KEY_LEFT_ARROW) then Sprite.Hide (standR) Sprite.Hide (standL) end if end loop |
Author: | Insectoid [ Mon Apr 06, 2015 11:46 pm ] | ||
Post subject: | RE:Making my character move left and right using sprites | ||
You don't need to use Sprite.ChangePic when using Sprite.Animate, since the latter does it for you. You only need one sprite per on-screen character, no matter how many pictures there are for that character. This means you only call Sprite.New once if you only have one character. Load all pictures for your sprite with Pic.FileNew before your main game loop. Use Sprite.Animate to both move your sprite and change the pic- you shouldn't need to use Sprite.Hide to change the pic. To move your sprite, just change the x or y values and call Sprite.Animate, which will redraw the sprite at the new coordinates. For example,
|
Author: | dragonboy456 [ Tue Apr 07, 2015 8:02 am ] |
Post subject: | Re: Making my character move left and right using sprites |
Thanks for the help. I have I guess you could say improved my code. I was just wondering if this is the only way that you can do this. Like, can I change the code so that the same thing happens. And one last think, is there something I can add to my code so that the hulk doesnt flash or "blink after it moves to the right and left. Thanks again %Declare Variables var chars : array char of boolean var pics : array 0 .. 6 of int var sprite, standR, standRID, standL, standLID : int var x,y : int x:=200 y:=200 %Initialize standing sprite standRID := Pic.FileNew ("lookRight.bmp") standR := Sprite.New (standRID) standLID := Pic.FileNew ("lookLeft.bmp") standL := Sprite.New (standLID) %Starting picture Sprite.SetPosition (standL, x,y, true) Sprite.Show (standL) % %For each key press repeat % loop Input.KeyDown (chars) % % %If the user presses the right arrow key % if chars (KEY_RIGHT_ARROW) then % %Hides the standing still sprite % Sprite.Hide (standR) Sprite.Hide (standL) % % Loading each picture into the array % for i : 1 .. 6 x := x + 5 pics (i - 1) := Pic.FileNew ("right" + intstr (i) + ".bmp") delay (20) if Error.Last not= 0 then put "Error loading image: ", Error.LastMsg return end if end for sprite := Sprite.New (pics (0)) % %Animate picture % for i : 1 .. 6 Sprite.ChangePic (sprite, pics (i mod 6)) Sprite.SetPosition (sprite, x,y, true) Sprite.Show (sprite) delay (80) end for Sprite.Free (sprite) Sprite.SetPosition (standR, x,y, true) Sprite.Show (standR) end if % %If the user presses the left arrow key % if chars (KEY_LEFT_ARROW) then % %Hides the standing still sprite % Sprite.Hide (standL) Sprite.Hide (standR) % % Loading each picture into the array % for i : 1 .. 6 x := x - 5 pics (i - 1) := Pic.FileNew ("left" + intstr (i) + ".bmp") if Error.Last not= 0 then put "Error loading image: ", Error.LastMsg return end if end for sprite := Sprite.New (pics (0)) Sprite.SetPosition (sprite, x,y, true) Sprite.Show (sprite) % %Animate picture % for i : 1 .. 6 Sprite.ChangePic (sprite, pics (i mod 6)) delay (80) end for Sprite.Free (sprite) Sprite.SetPosition (standL, x,y, true) Sprite.Show (standL) end if % end loop |
Author: | Dreadnought [ Tue Apr 07, 2015 11:18 am ] |
Post subject: | Re: Making my character move left and right using sprites |
Here's a tutorial on eliminating flickering: http://compsci.ca/v3/viewtopic.php?t=12533 |
Author: | Insectoid [ Tue Apr 07, 2015 3:51 pm ] |
Post subject: | RE:Making my character move left and right using sprites |
Quote: I was just wondering if this is the only way that you can do this.
This is pretty much the only way to use sprites. I mean, you can manually use Sprite.SetHeight, Sprite.Show, Sprite.Move, etc, but Sprite.Animate does all of that already so why bother? You can also draw your images directly without using sprites at all. I prefer this approach myself. It takes a little bit more work but removes a lot of the magic (magic is things that you don't know why they work, but they do). Doing away with sprites lets you mix pictures and primitives (draw.line, draw.box, etc) together more easily and gives you some finer control over what's going on. I recommend you try both approaches and see what you like best. |
Author: | dragonboy456 [ Tue Apr 07, 2015 8:54 pm ] |
Post subject: | Re: Making my character move left and right using sprites |
WOW, this has been a lot of help. I just have one more question. I got my Hulk to animate but it doesn't walk from right to left and how do I make it so that when it punches, it punches in the way it is facing. So for example, if it is facing right, how does it punch right and vice versa. Is it possible to upload my entire file instead of just one by one? |
Author: | Insectoid [ Tue Apr 07, 2015 9:26 pm ] |
Post subject: | RE:Making my character move left and right using sprites |
Keep a variable the records which way your character is facing. When it punches, check this variable and choose the direction accordingly. |