Hit a road block trying to figure out how to correct when hitting a sprite from any side. I can correct left&right, or top&bottom, but not all at same time. This of course is trying to put the sprite side by side, and not leave such a gap between when dragging. I could go back to using the previous correcting way, but I like the smoother look of close together.
Here's what I have so far. The code in red (in the blocked function) is the problem I'm having a hard time figuring out. If I could figure out how to tell WHICH side the sprite is being hit from...
- Dav
Code: Select all
'freeblock.txt
'free red block from the room.
'move blue blocks out of the way.
'Code by Dav, JAN/2017
option base 1
graphics
set orientation top
get screen size sw,sh
refresh off
sprites=7
'draw red box sprite
graphics clear 1,0,0
draw rect 2,2 to 98,198
sprite "1" scan 0,0,100,200
sprite "1" show
sprite "1" at 51,51
'draw blue box sprites
dx=110!dy=300
for a = 2 to sprites
graphics clear 0,0,1
draw rect 2,2 to 98,198
sprite str$(a) scan 0,0,100,200
sprite str$(a) show
sprite str$(a) at dx,dy
dx=dx+110
if dx>sw-50-110 then
dx=110!dy=dy+300
end if
next a
graphics clear 0,0,0
'draw exit sprite
sprite "exit" scan 0,0,110,50
sprite "exit" show
sprite "exit" at sw/2-55,sh-50
'draw walls
fill color .3,.3,.3
fill rect 0,0 to sw,50 'top
fill rect 0,0 to 50,sh 'left
fill rect sw-50,0 to sw,sh 'right
fill rect 0,sh-50 to sw,sh 'bottom
refresh on
do
get touch 0 as tx,ty 'get first touch
for a = 1 to sprites
'if user presses on the sprite
if sprite_hit(str$(a), tx,ty) then
again:
'get sprite x,y location
get sprite str$(a) pos sx,sy
do
get touch 0 as tx2,ty2 'get finger drag
if tx2=-1 then break 'break loop if up
get sprite str$(a) pos sx2,sy2
sprite str$(a) at tx2-tx+sx,ty2-ty+sy
'see if red box hits exit sprite
if sprites_collide("1","exit") then
sprites delete
end
end if
'see if red sprite hits another sprite
if blocked then
'sprite str$(a) at sx2,sy2
click ! break
end if
'make sure sprite stays within walls
get sprite str$(a) pos lx,ly
get sprite str$(a) size lsx,lsy
'check left
if lx<50 then
sprite str$(a) at 50,sy2
click ! break
end if
'check right
if lx>sw-50-lsx then
sprite str$(a) at sw-50-lsx,sy2
click ! break
end if
'check top
if ly<50 then
sprite str$(a) at sx2,50
click ! break
end if
'check bottom
if ly> sh-50-lsy then
sprite str$(a) at sx2,sh-50-lsy
click ! break
end if
until 0
if touch_x(0)>-1 then
get touch 0 as tx,ty
goto again 'keep on same sprite
end if
end if
next a
slowdown
until 0
def blocked()
for t= 1 to .sprites
if t= .a then goto skip
if sprites_collide(str$(.a),str$(t)) then
'r'
get sprite str$(.a) pos ax,ay
get sprite str$(.a) size asx,asy
get sprite str$(t) pos tx,ty
get sprite str$(t) size tsx,tsy
'need a better way to figure out
'which side to correct.
'if i could tell wich side of sprite
'was being hit, that would help...
'right & left seem to work.
'hitting sprite on right
if ax+asx>tx and tx>ax then
sprite str$(.a) at tx-asx,ay
end if
'hitting sprite on left
if ax<tx+tsx and tx < ax then
sprite str$(.a) at tx+tsx,ay
end if
'hitting sprite below
if ay+asy>ty and ay < ty then
'sprite str$(.a) at ax,ty-asy
end if
'hitting sprite on top
if ay<ty+tsy and ty>ay then
'sprite str$(.a) at ax,ty+tsy
end if
''
'sprite str$(.a) at .sx2,.sy2
return 1
end if
skip:
next t
blocked=0
end def
def click
notes set "113:tc8"
notes play
pause .1
end def