Animation and idle pose

BlitzMax Forums/BlitzMax Beginners Area/Animation and idle pose

ok the idea is that i click with the mouse and this code moves a image called block towards the target one pixel at a time cheaking one pixel ahead before moving.

before i move the character (indy) i store his postion as old_x and old_y, and the next time round the loop if the old xy is the same as the new xy i would like it to draw the idle indy image.

this code doesnt work ? it displays the idle pos but when i move left it just keeps playing the walk left anim even when he stops. also i can think of a way to have the idle image face the way indy was traveling last.




If old_x = block_xpos And old_y = block_ypos
	DrawImage(indy_r,block_xpos,block_ypos)
	Else
		If old_x > block_xpos
		DrawImage(indy_wl,block_xpos,block_ypos,frame)
			If MilliSecs() >tmr + anim_speed
				tmr = MilliSecs()
				frame = frame +1
					If frame = 6 Then
					frame =0
					EndIf
			EndIf
		EndIf
EndIf
ResetCollisions()
'move up
If block_ypos > ytarget
	If ImagesCollide(block,block_xpos,block_ypos-1,0,walkable_area,40,40,0)= 0
	old_y = block_ypos
	block_ypos:- 1
	EndIf	
EndIf
ResetCollisions()

'move down
If block_ypos < ytarget
	If ImagesCollide(block,block_xpos,block_ypos+1,0,walkable_area,40,40,0)= 0
	old_y = block_ypos
	block_ypos:+ 1
	EndIf	
EndIf
ResetCollisions()

'move left
If block_xpos > xtarget
	If ImagesCollide(block,block_xpos-1,block_ypos,0,walkable_area,40,40,0)= 0   
	old_x = block_xpos
	block_xpos:- 1
	EndIf
EndIf
ResetCollisions()

'move right
If block_xpos < xtarget
	If ImagesCollide(block,block_xpos+1,block_ypos,0,walkable_area,40,40,0)= 0
	old_x = block_xpos
	block_xpos:+ 1
	EndIf
EndIf
ResetCollisions()


<edit> Scrub all that.
<edit> Scrub again...
You might want to move your old_x=block_xpos assign to the end of your first block so you don't have to do 5-6 times. Same with your resetcollisions.
You code is poorly laid out but I can't see where the problem is from it.
This does what you want (in simple terms) and might help but, even if you don't use OO, you need to check into functional programming.
SuperStrict
Graphics 800,600
Local old_x:Int
Local old_y:Int
Local block_xpos:Int=400
Local block_ypos:Int=500
While Not KeyHit(KEY_ESCAPE)
   Cls
   If old_y=block_ypos And old_x=block_xpos
	   DrawOval block_xpos-4,block_ypos-4,8,4
   Else
        DrawOval block_xpos-4,block_ypos-4,8,8
   EndIf
   old_y=block_ypos
   old_x=block_xpos
   If KeyDown(KEY_UP) block_ypos:-1
   Flip
Wend   
   


is indy_r the idle image, and is it a single image or does it consist of frames ( idle animation )?

what is it that doesn't work? it doesn't display the idle image, the blocks don't move?

would it be possible to zip up the source and resources and post a link?
i'd like to take a look at the complete code and play with it to see if i can uderstand what you mean. if not i understand.

Edit:
hmmm had to walk away for a few mins when i was typing this and by the time i finally posted tonyg had already respond. i'd still like to have a play with the source and resources if possible though.

TonyG: Thanks, but my code works by moving a character towards the point where the mouse was clicked so i can use if keydown etc. plus cls would get rid of the background? I know i need to learn more about how my code is layed out. i will put all the update character code in a function when i know it works.

KingNothing: ok ill pack it up when i get back home and post a link.

(link removed! source for finish game will be released to anyone that wants it when finished)

this is just my test of animation and collision plus the need media

but my code works by moving a character towards the point where the mouse was clicked so i can use if keydown etc
Yes, I know. I wasn't replacing your code but showing that your ideas does work.
plus cls would get rid of the background?
It would. If you're redrawing every tile each time anyway then you don't need the cls. Again, I wasn't giving you an answer.
What is it you're looking for? Pointers in the right direction, a fix for this particular problem or some code?
Anyway, move your assignment of old_x and old_y up your code...
If old_x = block_xpos And old_y = block_ypos
	DrawImage(indy_r,block_xpos,block_ypos)
	Else
		If  old_x > block_xpos 
		DrawImage(indy_wl,block_xpos,block_ypos,frame)
			If MilliSecs() >tmr + anim_speed
				tmr = MilliSecs()
				frame = frame +1
					If frame = 6 Then
					frame =0
					EndIf
			EndIf
		EndIf
	
EndIf
ResetCollisions()
old_y = block_ypos
old_x = block_xpos

'move up
If block_ypos > ytarget
	If ImagesCollide(block,block_xpos,block_ypos-1,0,walkable_area,40,40,0)= 0
	block_ypos:- 1

	EndIf	
EndIf
ResetCollisions()

'move down
If block_ypos < ytarget
	If ImagesCollide(block,block_xpos,block_ypos+1,0,walkable_area,40,40,0)= 0
	block_ypos:+ 1

	EndIf	
EndIf
ResetCollisions()

'move left
If block_xpos > xtarget
	If ImagesCollide(block,block_xpos-1,block_ypos,0,walkable_area,40,40,0)= 0   
	block_xpos:- 1

	EndIf
EndIf
ResetCollisions()

'move right
If block_xpos < xtarget
	If ImagesCollide(block,block_xpos+1,block_ypos,0,walkable_area,40,40,0)= 0
	block_xpos:+ 1

	EndIf
EndIf
ResetCollisions()


first of all sorry it took so long, right after my last post i ended up playing some gta for a very long time.

ok i think i have what your looking for now, it was relatively easy. i just took a few min to study your code and see what you were trying to do and how you were trying to do it. after a few more min i came up with what i think/hope you were asking for. i only changed a small percentage of your code slightly but you'll notice whats different, i stayed as close to your style as i could. i remarked what i added as '*******New*****.
anyway here's the new code


'   collison / animation testing
AutoImageFlags 0
Const width =640
Const hieght = 480
Const colour_depth = 0
 
AppTitle = "animtest"
Graphics width,hieght,colour_depth
Global hWnd% = GetActiveWindow() ' Save current Window handle

'timer
Global tmr:Int = MilliSecs()
Global frame:Int=0
Global anim_speed:Int=100

boarder = LoadImage ("level/boarder.bmp")
'background = LoadImage ("level/final back ground5.bmp")
walkable_area =LoadImage ("level/final back walkable2.png")
block=LoadImage ("level/colideblock.bmp")
pointer = LoadImage ("level/pointer.png")
indy_r = LoadImage ("level/sprites/indy_r.png")
indy_wr = LoadAnimImage("level/sprites/indy_wr.png",32,64,0,6)
indy_wl = LoadAnimImage("level/sprites/indy_wl.png",32,64,0,6)

'******New*****
indy_wu = LoadAnimImage("level/sprites/indy_wu.png",32,64,0,4)
indy_wd = LoadAnimImage("level/sprites/indy_wd.png",32,64,0,4)
'**************

SetImageHandle indy_r,16,60
SetImageHandle indy_wr,16,60
SetImageHandle indy_wl,16,60
SetImageHandle block,8,4
Global block_xpos:Int = 300
Global block_ypos:Int = 230 

Global old_x:Int = block_xpos
Global old_y:Int = block_ypos

Global xtarget:Int = 300
Global ytarget:Int = 230

Global scale:Float = 1.1
Global offset:Int = 0
HideMouse()

 

While Not KeyHit(key_escape)

If MouseHit(1) 
	frame = 0	'*****New*****
	xtarget = MouseX() 
	ytarget = MouseY()
EndIf

DrawImage(boarder,0,0)
SetViewport(40,40,560,420)'set to limit level to inside boarder 560x220.
DrawImage(walkable_area,40 + offset,40)
DrawImage(block,block_xpos + offset,block_ypos)
'DrawImage(background,40 + offset,40)
SetViewport(0,0,640,480) 'set back for mouse over


If old_x = block_xpos And old_y = block_ypos
	DrawImage(indy_r,block_xpos,block_ypos)
Else
	If  old_x > block_xpos 			
		If MilliSecs() >tmr + anim_speed
			tmr = MilliSecs()
			frame = frame +1
		EndIf
		If frame = 6 Then frame =0
		DrawImage(indy_wl,block_xpos,block_ypos,frame)
	EndIf
	
'******New*****

	If  old_x < block_xpos 			
		If MilliSecs() >tmr + anim_speed
			tmr = MilliSecs()
			frame = frame +1
		EndIf
		If frame = 6 Then frame =0
		DrawImage(indy_wr,block_xpos,block_ypos,frame)
	EndIf	
	
	If  old_x = block_xpos And old_y < block_ypos
		If MilliSecs() >tmr + anim_speed
			tmr = MilliSecs()
			frame = frame +1
		EndIf
		If frame > 3 Then frame =0
		'i had to add on x and y offset for the up/down images
		' because your not using midhandleand i didn't want to modify your code too much.
		DrawImage(indy_wd,block_xpos-15,block_ypos-60,frame)	
	EndIf
	
	If  old_x = block_xpos And old_y > block_ypos
		If MilliSecs() >tmr + anim_speed
			tmr = MilliSecs()
			frame = frame +1
		EndIf
		If frame > 3 Then frame =0
		DrawImage(indy_wu,block_xpos-15,block_ypos-60,frame)	
	EndIf
	
'**************	
	
EndIf
ResetCollisions()
old_y = block_ypos
old_x = block_xpos

'move up
If block_ypos > ytarget
	If ImagesCollide(block,block_xpos,block_ypos-1,0,walkable_area,40,40,0)= 0
	block_ypos:- 1

	EndIf	
EndIf
ResetCollisions()

'move down
If block_ypos < ytarget
	If ImagesCollide(block,block_xpos,block_ypos+1,0,walkable_area,40,40,0)= 0
	block_ypos:+ 1

	EndIf	
EndIf
ResetCollisions()

'move left
If block_xpos > xtarget
	If ImagesCollide(block,block_xpos-1,block_ypos,0,walkable_area,40,40,0)= 0   
	block_xpos:- 1

	EndIf
EndIf
ResetCollisions()

'move right
If block_xpos < xtarget
	If ImagesCollide(block,block_xpos+1,block_ypos,0,walkable_area,40,40,0)= 0
	block_xpos:+ 1

	EndIf
EndIf
ResetCollisions()
ResetCollisions()

Local m_x:Int = MouseX()
Local m_y:Int = MouseY()
DrawImage(pointer,m_x:Int,m_y:Int)

' close window button now works ###########	
If AppTerminate() Then 
       ' insert garbage clear up here <<<<<
       End 
    EndIf 
'##########################################
Flip
Cls
Wend




i hope that helps ya out.

as a side note:
i'm gonna have to agree with tony. doing small simple programs like that is one thing. but when you get into making larger programs, it'll be hell to read and follow. you need to structure your code. start by just using functions and then later on get into using types with methods and functions, you'll do your self a huge favour.

i know from expierence, when i first started coding thats exactly how i did it. its a very bad habbit and the longer one codes with out structure the harder it is to break that habbit.

thanks thats spot on! yes i agree with both of you i need to start using structure, Part of this was that i havent used blitzmax before, I didnt really understand how everything works. i should have read the sloan book all the way through but the compo dead line is sunday and i want to get something in by then,even if it is a couple of screens and a puzzle demo.

KingNothing thanks again for taking the time to do this!

Now ive just got to squeeze the scaling in ;OP

np, glad i could help