I'd be really grateful if someone would give me some help. I am wrting a test program, that allows you to push blocks around a screen, but unfortunately it doesn't quite work, and I'm not sure why. I'm sure its something obvious but I can't see it and I was hoping someone else would be able to help. Is there a realtime debugger in Max, because that would be really useful. I looked on help, but it doesn't mention anything.
Here's the program if someone wouldn't mind running it. You control the white square with the 'z','x',':' and '.' keys (left/right/up/down) and you can push the other blocks around the screen.
You can also pick up the blocks with the mouse (left mouse button) and put them somewhere else (right mouse button to drop). Anyway its a bit clunky but I'm just trying to get something simple working at the moment.
The problem comes when you try to push a red block onto a yellow block. Red and yellow are objects 2 and 3 in the list, so I presume that this may be something to do with why this isn't working. Other combinations all seem to work as far as I can tell.
Please help - heres the code - you can run it because it creates its own gfx. Thank you! (I just cut and pasted this so it may lose some identing - I don't know how to do it properly - sorry)
Rico
-----------------------CODE BELOW-----------------
Graphics 1024,768,16
'--------------
DrawRect 0,0,128,32
image:TImage=CreateImage(32,32) 'square block
image2:TImage=CreateImage(4,4) 'pointer
GrabImage(image,0,0,0)
GrabImage(image2,0,0,0)
Cls
SetBlend SOLIDBLEND
Type block
Field x,y,xv,yv,id
EndType
Global block_list:TList= New TList
Global num_block=4
Global p:block
Global ir=0
For m=1 To num_block
b:block=New block
ReadData b.id,b.x,b.y,b.xv,b.yv
If b.id=1 Then p=b 'player block set to first object
block_list.addlast b
Next
Global sel:block=Null
gr=0;drwpt=True
While Not KeyDown(KEY_ESCAPE)
Cls
mx=MouseX();my=MouseY()
lb=MouseDown(1);rb=MouseDown(2)
DrawText " px "+p.x+" py "+p.y,0,0
If sel=Null Then ps=0 Else ps=1
DrawText " lb "+lb+" sel "+ps,0,16
lk=KeyDown(KEY_Z);rk=KeyDown(KEY_X)
uk=KeyDown(KEY_SEMICOLON);dk=KeyDown(KEY_PERIOD)
'------mouse selection and player movement
For b=EachIn block_list
If lb And sel=Null
hit=ptinrect(mx,my,b.x,b.y,b.x+31,b.y+31)
If hit
sel=b
xoff=mx-b.x ; yoff=my-b.y
EndIf
EndIf
If rb And sel=b
b.x=b.x-xoff;b.y=b.y-yoff
xoff=0;yoff=0
sel=Null
EndIf
If b=sel Then b.x=mx;b.y=my;drwpt=False
b.xv=0;b.yv=0
If b=p
b.xv=0;b.yv=0
If lk Then b.xv=-1
If rk Then b.xv=1
If uk Then b.yv=-1
If dk Then b.yv=1
EndIf
Next
'--------- collision detection
ir=0
For b=EachIn block_list
If b<>sel Then moveobject(b)
Next
'------draw images
If drwpt Then DrawImage image2,mx,my,0 ' draw mouse pointer
c=0
For b=EachIn block_list
c=c+1
Select c 'draw each block a different colour
Case 1 SetColor 255,255,255
Case 2 SetColor 255,0,0
Case 3 SetColor 255,255,0
Case 4 SetColor 0,0,255
EndSelect
If sel=b
DrawImage image,b.x-xoff,b.y-yoff,0
Else
DrawImage image,b.x,b.y,0
EndIf
Next
SetColor 255,255,255
Flip 1
Wend
End
Function moveobject(bl:block)
Local cond=True
Local b2:block=New block
ir=ir+1
For b2=EachIn block_list
If b2<>bl And b2<>sel
nx=bl.x+bl.xv; ny=bl.y+bl.yv
nbx=b2.x+b2.xv; nby=b2.y+b2.yv
If rectsoverlap(nx,ny,31,31,nbx,nby,31,31)
b2.xv=bl.xv;b2.yv=bl.yv
cond=moveobject(b2)
EndIf
EndIf
Next
bl.x=bl.x+bl.xv; bl.y=bl.y+bl.yv
b2=Null
If ir>60 Then Print "Infinite Recursion? ";End
Return cond
End Function
Function rectsoverlap:Int(x0,y0,w0,h0,x2,y2,w2,h2)
If x0>(x2+w2)Or(x0+w0)<x2 Then Return False
If y0>(y2+h2)Or(y0+h0)<y2 Then Return False
Return True
End Function
Function ptinrect:Int(px,py,x0,y0,x2,y2)
If px>x0 And px<x2 And py>y0 And py<y2 Then Return True
Return False
End Function
DefData 1,60,400,0,0
DefData 2,100,100,0,0
DefData 3,99,200,0,0
DefData 4,132,300,0,0
DefData 5,160,330,0,0
DefData 6,192,500,0,0
Here's the program if someone wouldn't mind running it. You control the white square with the 'z','x',':' and '.' keys (left/right/up/down) and you can push the other blocks around the screen.
You can also pick up the blocks with the mouse (left mouse button) and put them somewhere else (right mouse button to drop). Anyway its a bit clunky but I'm just trying to get something simple working at the moment.
The problem comes when you try to push a red block onto a yellow block. Red and yellow are objects 2 and 3 in the list, so I presume that this may be something to do with why this isn't working. Other combinations all seem to work as far as I can tell.
Please help - heres the code - you can run it because it creates its own gfx. Thank you! (I just cut and pasted this so it may lose some identing - I don't know how to do it properly - sorry)
Rico
-----------------------CODE BELOW-----------------
Graphics 1024,768,16
'--------------
DrawRect 0,0,128,32
image:TImage=CreateImage(32,32) 'square block
image2:TImage=CreateImage(4,4) 'pointer
GrabImage(image,0,0,0)
GrabImage(image2,0,0,0)
Cls
SetBlend SOLIDBLEND
Type block
Field x,y,xv,yv,id
EndType
Global block_list:TList= New TList
Global num_block=4
Global p:block
Global ir=0
For m=1 To num_block
b:block=New block
ReadData b.id,b.x,b.y,b.xv,b.yv
If b.id=1 Then p=b 'player block set to first object
block_list.addlast b
Next
Global sel:block=Null
gr=0;drwpt=True
While Not KeyDown(KEY_ESCAPE)
Cls
mx=MouseX();my=MouseY()
lb=MouseDown(1);rb=MouseDown(2)
DrawText " px "+p.x+" py "+p.y,0,0
If sel=Null Then ps=0 Else ps=1
DrawText " lb "+lb+" sel "+ps,0,16
lk=KeyDown(KEY_Z);rk=KeyDown(KEY_X)
uk=KeyDown(KEY_SEMICOLON);dk=KeyDown(KEY_PERIOD)
'------mouse selection and player movement
For b=EachIn block_list
If lb And sel=Null
hit=ptinrect(mx,my,b.x,b.y,b.x+31,b.y+31)
If hit
sel=b
xoff=mx-b.x ; yoff=my-b.y
EndIf
EndIf
If rb And sel=b
b.x=b.x-xoff;b.y=b.y-yoff
xoff=0;yoff=0
sel=Null
EndIf
If b=sel Then b.x=mx;b.y=my;drwpt=False
b.xv=0;b.yv=0
If b=p
b.xv=0;b.yv=0
If lk Then b.xv=-1
If rk Then b.xv=1
If uk Then b.yv=-1
If dk Then b.yv=1
EndIf
Next
'--------- collision detection
ir=0
For b=EachIn block_list
If b<>sel Then moveobject(b)
Next
'------draw images
If drwpt Then DrawImage image2,mx,my,0 ' draw mouse pointer
c=0
For b=EachIn block_list
c=c+1
Select c 'draw each block a different colour
Case 1 SetColor 255,255,255
Case 2 SetColor 255,0,0
Case 3 SetColor 255,255,0
Case 4 SetColor 0,0,255
EndSelect
If sel=b
DrawImage image,b.x-xoff,b.y-yoff,0
Else
DrawImage image,b.x,b.y,0
EndIf
Next
SetColor 255,255,255
Flip 1
Wend
End
Function moveobject(bl:block)
Local cond=True
Local b2:block=New block
ir=ir+1
For b2=EachIn block_list
If b2<>bl And b2<>sel
nx=bl.x+bl.xv; ny=bl.y+bl.yv
nbx=b2.x+b2.xv; nby=b2.y+b2.yv
If rectsoverlap(nx,ny,31,31,nbx,nby,31,31)
b2.xv=bl.xv;b2.yv=bl.yv
cond=moveobject(b2)
EndIf
EndIf
Next
bl.x=bl.x+bl.xv; bl.y=bl.y+bl.yv
b2=Null
If ir>60 Then Print "Infinite Recursion? ";End
Return cond
End Function
Function rectsoverlap:Int(x0,y0,w0,h0,x2,y2,w2,h2)
If x0>(x2+w2)Or(x0+w0)<x2 Then Return False
If y0>(y2+h2)Or(y0+h0)<y2 Then Return False
Return True
End Function
Function ptinrect:Int(px,py,x0,y0,x2,y2)
If px>x0 And px<x2 And py>y0 And py<y2 Then Return True
Return False
End Function
DefData 1,60,400,0,0
DefData 2,100,100,0,0
DefData 3,99,200,0,0
DefData 4,132,300,0,0
DefData 5,160,330,0,0
DefData 6,192,500,0,0