My frames are not acting right
BlitzPlus Forums/BlitzPlus Beginners Area/My frames are not acting right
| ||
I'm still a beginner in game programming. It has been a long time since I have posted here. My question regards animation. In my game code below, I'm wondering why when I press a button (key 25), the character does other things in the frame before it does what I want it to do (punch)? While I'd also like to know why my character doesn't do what I wanted instantly, I also would like to know how to get it so that I can just press one key for my character to punch (in the code below, I got it so that one key [key 25] is used to punch in the right direction and another key [key 24] is used to punch in the left direction - but, I'd like to eventually change that and make it all be done with one key). Thanks Graphics 1640, 1000 Global HENRY ;load images HENRY = LoadAnimImage ("HenryMoveAroundIII.PNG",400,370,0,14) LADDER = LoadImage ("Ladder.png") GOUPLADDER = LoadSound ("Thump.wav") ;define types Type HENRY Field x,y Field frame End Type Type LADDER Field x,y End Type ;set where the characters will start on screen h.HENRY = New HENRY h\x = 500 h\y = 200 h\frame = 0 l.LADDER = New LADDER l\x = 500 l\y = 25 While Not KeyDown(1) Cls ;these lines below initiate the punch, but everytime key (25) is hit, Henry doesn't instantly punch. He cycles through other ;standstill frames before actually punching. I wonder why? If KeyHit(25) h\frame = h\frame + 1 If h\frame > 7 h\frame = 6 EndIf EndIf ;I have the same problem here as I do with Key (25). When key (25) is pressed, Henry goes through other still frames instead of ;doing only the punch frame. If KeyHit(24) Then h\frame = h\frame +1 If h\frame > 9 h\frame = 8 EndIf EndIf ;This works perfectly. When key (205) e.g. the arrow key is pressed, Henry instantly walks across the screen and show him walking ;the first time the arrow key is pressed. Thus, there's no problem here If KeyDown(205) h\x = h\x + 3 h\frame = h\frame + 1 If h\frame > 2 Then h\frame = 1 EndIf EndIf ;this also works perfectly. So thus, I wonder why Keys (24) and (25) are not executing as instant as keys (205) and (206) are. If KeyDown(203) h\x = h\x - 3 ; h\frame = h\frame + 1 If h\frame > 5 Then h\frame = 4 EndIf EndIf ;finally, I'd like for the punches to be done with only one key (e.g. key 25). I will elimnate key (24) eventually, ;but I still want to know why it isn't working the way I want it to work? If ImagesCollide (HENRY, h\x, h\y,0,LADDER, l\x, l\y,0) Then If KeyHit (200) Then PlaySound (GOUPLADDER) h\y = h\y - 40 h\frame = h\frame + 1 If h\frame > 11 Then h\frame = 10 EndIf EndIf If KeyHit (208) Then PlaySound (GOUPLADDER) h\y = h\y + 40 h\frame = h\frame + 1 If h\frame > 13 Then h\frame = 12 EndIf EndIf EndIf DrawImage (LADDER, l\x,l\y) DrawImage (HENRY,h\x,h\y,h\frame) Flip Wend |
| ||
Hi, Without seeing the animation strip itself, it would be hard to tell what frames would need to be cycled. However, the process I would use would be something like this. Function AnimatePunchLeft() If(animate_punch_flag = False) Then animate_punch_flag = True; animate_punch_start_frame = 4; animate_punch_end_frame = 7; frame = animate_punch_start_frame; EndIf End Function Function AnimatePunchRight() If(animate_punch_flag = False) Then animate_punch_flag = True; animate_punch_start_frame = 8; animate_punch_end_frame = 11; frame = animate_punch_start_frame; EndIf End Function Function DrawPlayer() If(animate_punch_flag = True) Then frame = frame + 1 If (frame >= animate_punch_end_frame) Then animate_punch_flag = false; frame = 0 (or whatever the "normal" frame is) End If DrawImage(player, x,y, frame) Else DrawImage(blah, x,y frame) - no animation in this bit End If End Function Now in Main Loop **MAIN LOOP** If(KeyHit(24)) Then AnimatePunchLeft() EndIf If(KeyHit(25)) Then AnimatePunchRight() EndIf DrawPlayer() **MAIN LOOP** At least something along these lines. This will stop repeated key presses cutting the animation if it's greater than one single frame. Note: It's assumed that the end frame is always greater than the start frame. :D Last edited 2011 |
| ||
Hey Yeshu777. Long time no see (I haven't been here for a while because I moved to China). Thanks for helping me. I have yet to learn how to work with functions and I'm going to implement what you said and will tell you how it goes. Last edited 2011 |
| ||
No problem, it's only rough outline as to a possible method. Best Regards, |
| ||
Hey Yeshu, one person has helped me with my code. But can you help me with the second part of my question. It's on the same thread and it's here in the link below: http://www.blitzbasic.com/Community/posts.php?topic=95265 My questions is at the bottom of the thread. Last edited 2011 |