Switching between .bmp images to create running effect(
Author |
Message |
PiratesCoder
|
Posted: Wed Jun 09, 2010 5:51 pm Post subject: Switching between .bmp images to create running effect( |
|
|
What is it you are trying to achieve?
To get the character to look like he's running.
What is the problem you are having?
I have many images of the same size, and when flipped through fast he looks like he's running. (By the way, this is Pirates of the Caribbean, so Sparrow is Jack Sparrow character image). I can't get past the first image, instead, it's stuck on image (1). He's moving but not changing frames
Describe what you have tried to solve this problem
For loops that add the image number, creating variables, etc. Nothing works
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
var key : array char of boolean
var screen := Pic.FileNew ("LevelOne1.jpg")
var picSparrow := Pic.FileNew ("CJS0.JPG")
Pic.Draw (picSparrow, 0, 50, picMerge)
var spriteSparrow := Sprite.New (picSparrow )
Sprite.Show (spriteSparrow )
% Create a starting point for the character
var sparrowX := 20
var sparrowY := 40
Pic.ScreenLoad ("LevelOne1.jpg", 0, 0, picCopy)
var images : array - 5 .. 5 of int
%Set Up Array
images (- 5) := Pic.FileNew ("C.J.S.-5.bmp")
images (- 4) := Pic.FileNew ("C.J.S.-4.bmp")
images (- 3) := Pic.FileNew ("C.J.S.-3.bmp")
images (- 2) := Pic.FileNew ("C.J.S.-2.bmp")
images (- 1) := Pic.FileNew ("C.J.S.-1.bmp")
images (0) := Pic.FileNew ("C.J.S.0.bmp")
images (1) := Pic.FileNew ("C.J.S.1.bmp")
images (2) := Pic.FileNew ("C.J.S.2.bmp")
images (3) := Pic.FileNew ("C.J.S.3.bmp")
images (4) := Pic.FileNew ("C.J.S.4.bmp")
images (5) := Pic.FileNew ("C.J.S.5.bmp")
loop
Input.KeyDown (key )
Sprite.SetPosition (spriteSparrow, sparrowX, sparrowY, true)
if key (KEY_RIGHT_ARROW) then
for i : 1 .. 4
if images (i ) = 4 or images (i ) < 0 then
images (i ) := 1
else
images (i ) + = 1
Sprite.ChangePic (spriteSparrow, images (i ))
end if
end for
sparrowX + = 1
elsif key (KEY_LEFT_ARROW) then
for decreasing l : - 1 .. - 4
if images (l ) = - 4 or images (l ) > 0 then
images (l ) := - 1
else
images (l ) - = 1
end if
end for
sparrowX - = 1
end if
View.Update
delay (10)
end loop
|
Please specify what version of Turing you are using
Turing 4.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Cezna

|
Posted: Wed Jun 09, 2010 6:34 pm Post subject: RE:Switching between .bmp images to create running effect( |
|
|
Instead of doing this in Turing, I would recommend loading the files into a program like Power Point and making a .gif out of them, and then using this in Turing.
It will run much faster, require less code, and be easier to make.
But, looking at your code, I would say that this part is your problem:
[code="Turing']
for i : 1 .. 4
if images (i) = 4 or images (i) < 0 then
images (i) := 1
else
images (i) += 1
Sprite.ChangePic (spriteSparrow, images (i))
end if
end for
[/code]
It is cycling through 4 times, adding 1 each time, and by the time it gets out of the for loop, it is back at 1.
So it is starting at 1, then since it is not 4 or 0, it is going to 2 (cycle 1), going to 3 (cycle 2), going to 4 (cycle 3), then since it is four, it is going to 1 (cycle 4), and then, since it is done the fourth cycle, and the for loop is for a count of 4, it exits, and he is left at 1.
That last part made a lot more sense in my head, but it looks at little confusing in the quick reply window, so if you don't get what I mean, post back and I will try to explain myself better).
Anyway, I don't know why you have a for loop for the movement, but as it appears from this code that it is unnecessary, I would recommend removing it. (or just using a .gif like I originally suggested). |
|
|
|
|
 |
Zren

|
Posted: Wed Jun 09, 2010 6:36 pm Post subject: RE:Switching between .bmp images to create running effect( |
|
|
That because your only updating the screen once every loop. While your cycling through all the frames per loop, so it only shows the last frame. It shows the first one since your ah...hmmm.
Images is an array of ints yes, but your storing images in it, you seem to be treating them as numbers. images(i) will never = 4. Just try put images(i). It's probably be some wacky number, this references the image in memory.
Personally, I'd split each animation into it's own array, then have a variable for what state (specify the animation eg. standing/walking right), and what frame of the animation you on.
EDIT: Bollocks, I didn't realize the Sprite Module actually worked properly. Oh well.
EDIT 2: Nope it's still rather lame and could be improved.
Here's a quick example:
Turing: |
var standing : array 1 .. 8 of int
for i : 1 .. 8
cls
Draw.Oval (round (20 + 10 * cosd (i * 45)), round (20 + 10 * sind (i * 45)), 5, 5, black)
standing (i ) := Pic.New (0, 0, 40, 40)
end for
var sprite : int := Sprite.New (standing (lower (standing )))
Sprite.SetPosition (sprite, maxx div 2, maxy div 2, true)
Sprite.Show (sprite )
var state : int := 1
var frame : int := 1
View.Set ("graphics,offscreenonly")
loop
cls
if state = 1 then
frame + = 1
if frame > upper (standing ) then
frame := lower (standing )
end if
Sprite.ChangePic (sprite, standing (frame ))
end if
Time.Delay (50) %Max 20 FPS
View.Update
end loop
Sprite.Free (sprite )
|
|
|
|
|
|
 |
Cezna

|
Posted: Wed Jun 09, 2010 7:05 pm Post subject: RE:Switching between .bmp images to create running effect( |
|
|
Once again, Zren has managed to put it much better than I, and so I will only add on thing.
If the purpose of the for loop is to cycle all of the images in one cycle of the main loop (I just thought of that now), in order to prevent the image from ever getting stuck on one of the frames that has the character in mid-stride, but instead force him to always stop on the standing frame when the movement key is released, then there is a simple way to fix your problem.
Just put a View.UpdateArea line inside the for loop in which the images are being cycled through, and have it only include the area the image is being drawn in.
If you don't understand how to use View.UpdateArea, this is the syntax:
Using View.UpdateArea as opposed to View.Update will allow you to cycle through the images much faster, and only refresh the area inside the boundaries x1, y1, x2, y2.
If you want a better explanation, just click the the View.UpdateArea in the syntax I posted above (it should be underlined), and you will be taken to a page from the Turing help manual explaining the procedure.
|
|
|
|
|
 |
PiratesCoder
|
Posted: Thu Jun 10, 2010 6:54 am Post subject: RE:Switching between .bmp images to create running effect( |
|
|
images(i) will never = 4. Just try put images(i). It's probably be some wacky number, this references the image in memory.
Sorry, I'm a bit confused about that part...
And does this mean that i need a loop rather than a for loop since it exits for loops as soon as it has cycled through once?
And Cezna, to be honest I've never even heard of using powerpoints or .gif images in Turing.
Could you please briefly explain how that would work? |
|
|
|
|
 |
Cezna

|
Posted: Thu Jun 10, 2010 11:55 am Post subject: RE:Switching between .bmp images to create running effect( |
|
|
To use power point, just load all of the miages in power point, one per frame, and then just lick 'save as', and then save it as a .gif file.
As for the loop part, you can have a for loop, I realized it's purpose after a few minutes, all you need is a View.UpdateArea line within the for loop, with it's edges set to encompass just the image. |
|
|
|
|
 |
Zren

|
Posted: Thu Jun 10, 2010 4:35 pm Post subject: Re: RE:Switching between .bmp images to create running effect( |
|
|
PiratesCoder @ Thu Jun 10, 2010 6:54 am wrote: images(i) will never = 4. Just try put images(i). It's probably be some wacky number, this references the image in memory.
In order to load images into turing, you need a integer variable right?
var pic :int := Pic.New(0, 0, maxx, maxy)
Well when you load it, your actually telling turing to load the picture into memory, then gives an integer to call that part of memory. So what your actually storing in pic is only an index to another array controlled by Turing that holds the Picture. Here's a little demonstation.
Turing: |
View.Set ("graphics")
var pic : array 1 .. 100 of int
for i : 1 .. 100
pic (i ) := Pic.New (0, 0, maxx, maxy)
end for
View.Set ("text")
for i : 1 .. 100
put "Pic(", i , ") = ", pic (i )
end for
|
What your logic is thinking of is that images(i) instead of i.
Turing: | if i=4 or i < 0 then
i = 1
else
i += 1
%draw sprite (i)
end if |
You ought to just remove the for loops, and change the variables i and l to be the same variable and call it frame. |
|
|
|
|
 |
PiratesCoder
|
Posted: Thu Jun 10, 2010 9:14 pm Post subject: RE:Switching between .bmp images to create running effect( |
|
|
Thanks, Zren, I created a new variable and replaced 'i' and 'l'.
I haven't gotten it completely working, but I also think that one of my main problems is that I use Sprite.Show to show only spriteSparrow, and not any of the others
Basically, I don't know if I'm making much sense, but my Sprite.Show isn't reading from the array, but just that single spriteSparrow picture. Therefore, it will always only show the sprite that I've created, and none of the pictures from the array
So, do I have to change my Srite.Show statement? Or do I have to update the pictures in the for loops? If you don't understand what I'm trying to say, please post back and I'll try to explain
Maybe this isn't even an affecting factor, but I did think that part of code was a bit off. What do you think? Does the Sprite.Show line make a difference? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Zren

|
Posted: Sat Jun 12, 2010 11:30 pm Post subject: RE:Switching between .bmp images to create running effect( |
|
|
Sorry for the late reply, CompSci was busy for once and your topic got pushed down off the Recent Topics.
http://compsci.ca/holtsoft/doc/sprite_show.html
Sprite.Show() just makes the sprite viewable. From there you just change the sprite with Sprite.Change().
spriteSparrow is a Sprite, a Sprite is basically just a holder of a Pic that will automatically update itself when you change the Pic it holds with Sprite.ChangePic().
The reason you only see one pic is because of your logic. You run through the entire for loop before you update the screen. And at that, when i = 4, your logic wants it to show the first sprite (i=1).
My advice, ditch the for loops and try to figure it out from there. Useful practice learning would be to make another program that simulates a for loop, using the regular loop running from 1 .. 10. Then make that loop into an infinite loop instead of exiting. That's basically the logic needed for sprites. Even further, you could have it go from 1 .. 10 and jump from 10 to 50 then go to 50 .. 60, then jump back to 1 and resume to 1 .. 10. All with only one loop and conditionals (if statements). |
|
|
|
|
 |
|
|