The code I am posting should check for collision (pixelcol) on every sprite. Instead it is just checking the last number for collision. How can I fix it? Here is my code.
Main.dba
Rem Project: eCity Prototype
Rem Created: Wednesday, June 30, 2010
Rem ***** Main Source File *****
sync on
sync rate 60
global r
r = make vector2(1)
r = make vector2(2) : `Base vectors
r = make vector2(3) : `Temp vector
r = make vector2(4) : `Temp vector
`Initialize the base vectors
set vector2 1, 40, -20
set vector2 2, -40, -20
`Define origin
global oX
global oY
oX = screen width() / 2
oY = screen height() / 4 * 3 + 100
`Draw
global TilesX
global TilesY
TilesX = 10
TilesY = 9
global plotno
global plotx
global ploty
global plottilex
global plottiley
global plottype `Plot Types: (1) Residential | (2) Business | (3) Industry | (Other) Image number of special building.
global plotstage `Plot Stages: (1) Plotted | (2) Built (Make building number 2 soon.)
global plotpopulation
global plottaxes
global plotattraction `Does this bring in any tourist or residents?
global plotpowered `This variable and the three below do not matter until next version. ;)
global plotwatered
global plotnearstreet
global plotstreetdemand
plotno = 3
dim CityArray(10,10)
open to read 1,"files\setup.txt"
read string 1,experience$
read string 1,level$
read string 1,money$
read string 1,mayor$
read string 1,cityname$
global experience
global level
global money
global mname$
global cname$
experience = val(experience$)
level = val(level$)
money = val(money$)
mname$ = mayor$
cname$ = cityname$
close file 1
if file exist("files\private\TownHall.xy") = 1
open to read 1,"files\private\TownHall.xy"
read string 1,thallx$
read string 1,thally$
thallx = val(thallx$)
thally = val(thally$)
global thallexist
thallexist = 1
close file 1
endif
load image "media\plot.png",1
load image "media\property_forsale.png",2
load image "media\road.png",3
load image "media\Government.png",4
load image "media\Property.png",5
backdrop off
do
cls
hud()
print disallow
drawNewGrid()
GridFunctions()
sprite 1,200,250,3
if thallexist = 1 then sprite 2,thallx,thally,4
TownHall()
if keystate(19) = 1
plot = 1
global plottype
plottype = 1
endif
if plot = 1
if plottype = 1
sprite plotno,x vector2(3),y vector2(3),2
endif
for a = 1 to plotno-1
if pixelcol(plotno,a) = 1
print "Collision detected with ",a
disallow = 1
else
disallow = 0
endif
next a
if mouseclick() = 1 and disallow = 0
plotx = x vector2(3)
ploty = y vector2(3)
plottilex = snapX
plottilex = snapY
if plottype= 1
plotstage = 0
plotpopulation = 5
plottaxes = 25
plotattraction = 2
sprite plotno,plotx,ploty,2
endif
CityArray(plottilex,plottiley) = plotno
open to write 1,"files\private\plot"+str$(plotno)+".xy"
write string 1,str$(plotno)
write string 1,str$(plottype)
write string 1,str$(plotx)
write string 1,str$(ploty)
write string 1,str$(plottilex)
write string 1,str$(plottiley)
write string 1,str$(plotstage)
write string 1,str$(plotpopulation)
write string 1,str$(plottaxes)
write string 1,str$(plotattraction)
close file 1
if file exist("files\private\largestplot.sts") = 1 then delete file "files\private\largestplot.sts"
open to write 1,"files\private\largestplot.sts"
write string 1,str$(plotno)
close file 1
plotno = plotno + 1
plot = 0
endif
endif
set cursor 0,0
sync
loop
end
Functions.dba
Rem ***** Included Source File *****
function hud()
print "$"+str$(money)
print "Level: "+str$(level)
print "XP: "+str$(experience)
print coordX#, " : ", coordY#
print x vector2(3)
print y vector2(3)
print snapX
print snapY
print plotno
if thallexist = 0
text 0,500,"[G] Town Hall"
else
text 0,560,"Plot:"
text 50,560,"[R] Residential"
endif
center text 400,0,cname$
center text 400,15,"Mayor "+mname$
endfunction
function drawNewGrid()
`Along relative x direction
`Vector 3 will hold the beginning point, 4 will contain the end of the map in the Y-direction
copy vector2 3, 2 : multiply vector2 3, TilesY
set vector2 4, oX, oY : add vector2 3, 3, 4
for x = 0 to TilesX
line x vector2(4), y vector2(4), x vector2(3), y vector2(3)
add vector2 3, 3, 1
add vector2 4, 4, 1
next x
`Along relative y direction
`Vector 3 will hold the beginning point, 4 will contain the end of the map in the X-direction
copy vector2 3, 1 : multiply vector2 3, TilesX
set vector2 4, oX, oY : add vector2 3, 3, 4
for y = 0 to TilesY
line x vector2(4), y vector2(4), x vector2(3), y vector2(3)
add vector2 3, 3, 2
add vector2 4, 4, 2
next y
endfunction
function GridFunctions()
set vector2 3, mousex() - oX, mousey() - oY
d1# = dot product vector2(1, 3)
d2# = dot product vector2(2, 3)
d3# = dot product vector2(1, 2)
la# = length vector2(1)
lb# = length vector2(2)
global coordX#Rem Project: eCity Prototype
Rem Created: Wednesday, June 30, 2010
Rem ***** Main Source File *****
sync on
sync rate 60
global r
r = make vector2(1)
r = make vector2(2) : `Base vectors
r = make vector2(3) : `Temp vector
r = make vector2(4) : `Temp vector
`Initialize the base vectors
set vector2 1, 40, -20
set vector2 2, -40, -20
`Define origin
global oX
global oY
oX = screen width() / 2
oY = screen height() / 4 * 3 + 100
`Draw
global TilesX
global TilesY
TilesX = 10
TilesY = 9
global plotno
global plotx
global ploty
global plottilex
global plottiley
global plottype `Plot Types: (1) Residential | (2) Business | (3) Industry | (Other) Image number of special building.
global plotstage `Plot Stages: (1) Plotted | (2) Built (Make building number 2 soon.)
global plotpopulation
global plottaxes
global plotattraction `Does this bring in any tourist or residents?
global plotpowered `This variable and the three below do not matter until next version. ;)
global plotwatered
global plotnearstreet
global plotstreetdemand
plotno = 3
dim CityArray(10,10)
open to read 1,"files\setup.txt"
read string 1,experience$
read string 1,level$
read string 1,money$
read string 1,mayor$
read string 1,cityname$
global experience
global level
global money
global mname$
global cname$
experience = val(experience$)
level = val(level$)
money = val(money$)
mname$ = mayor$
cname$ = cityname$
close file 1
if file exist("files\private\TownHall.xy") = 1
open to read 1,"files\private\TownHall.xy"
read string 1,thallx$
read string 1,thally$
thallx = val(thallx$)
thally = val(thally$)
global thallexist
thallexist = 1
close file 1
endif
load image "media\plot.png",1
load image "media\property_forsale.png",2
load image "media\road.png",3
load image "media\Government.png",4
load image "media\Property.png",5
backdrop off
do
cls
hud()
print disallow
drawNewGrid()
GridFunctions()
sprite 1,200,250,3
if thallexist = 1 then sprite 2,thallx,thally,4
TownHall()
if keystate(19) = 1
plot = 1
global plottype
plottype = 1
endif
if plot = 1
if plottype = 1
sprite plotno,x vector2(3),y vector2(3),2
endif
for a = 1 to plotno-1
if pixelcol(plotno,a) = 1
print "Collision detected with ",a
disallow = 1
else
disallow = 0
endif
next a
if mouseclick() = 1 and disallow = 0
plotx = x vector2(3)
ploty = y vector2(3)
plottilex = snapX
plottilex = snapY
if plottype= 1
plotstage = 0
plotpopulation = 5
plottaxes = 25
plotattraction = 2
sprite plotno,plotx,ploty,2
endif
CityArray(plottilex,plottiley) = plotno
open to write 1,"files\private\plot"+str$(plotno)+".xy"
write string 1,str$(plotno)
write string 1,str$(plottype)
write string 1,str$(plotx)
write string 1,str$(ploty)
write string 1,str$(plottilex)
write string 1,str$(plottiley)
write string 1,str$(plotstage)
write string 1,str$(plotpopulation)
write string 1,str$(plottaxes)
write string 1,str$(plotattraction)
close file 1
if file exist("files\private\largestplot.sts") = 1 then delete file "files\private\largestplot.sts"
open to write 1,"files\private\largestplot.sts"
write string 1,str$(plotno)
close file 1
plotno = plotno + 1
plot = 0
endif
endif
set cursor 0,0
sync
loop
end
global coordY#
coordX# = (d1#*lb#*lb# - d2#*d3#) / ((la#*lb#)^2 - d3#^2)
coordY# = (d2#*la#*la# - d1#*d3#) / ((la#*lb#)^2 - d3#^2)
` 'Snapped' coordinates
global snapX
global snapY
snapX = int(coordX# + 0.5)
snapY = int(coordY# + 0.5)
set vector2 3, oX, oY
copy vector2 4, 1 : multiply vector2 4, snapX
add vector2 3, 3, 4
copy vector2 4, 2 : multiply vector2 4, snapY
add vector2 3, 3, 4
endfunction
function pixelcol(sprite1,sprite2)
if Sprite Collision(sprite1, sprite2)
collision as boolean
collision=0
memnum=1
while memblock exist(memnum)=1
memnum=memnum+1
endwhile
make memblock from image memnum,sprite image(sprite1)
memnum2=memnum
while memblock exist(memnum2)=1
memnum2=memnum2+1
endwhile
make memblock from image memnum2,sprite image(sprite2)
text 0,0, str$(memnum)
text 0,20, str$(memnum2)
if Sprite X(sprite2)<=Sprite X(sprite1)
s2x=sprite X(sprite1)-sprite x(sprite2)
s1x=0
endif
if Sprite X(sprite1)<Sprite X(sprite2)
s2x=0
s1x=sprite x(sprite2)-sprite x(sprite1)
endif
if Sprite y(sprite2)<=sprite y(sprite1)
s1y=0
s2y=sprite y(sprite1)-sprite y(sprite2)
endif
if sprite y(sprite1)<sprite y(sprite2)
s1y=sprite y(sprite2)-sprite y(sprite1)
s2y=0
endif
if sprite x(sprite1)+sprite width(sprite1)<=sprite x(sprite2)+sprite width(sprite2)
e1x=sprite width(sprite1)
e2x=sprite width(sprite2)-( sprite x(sprite2)+sprite width(sprite2)-( sprite x(sprite1)+sprite width(sprite1) ) )
endif
if sprite x(sprite2)+sprite width(sprite2)<sprite X(sprite1)+sprite width(sprite1)
e2x=sprite width(sprite2)
e1x=sprite width(sprite1)-( sprite x(sprite1)+sprite width(sprite1)-( sprite x(sprite2)+sprite width(sprite2) ) )
endif
if sprite y(sprite1)+sprite height(sprite1)<=sprite y(sprite2)+sprite height(sprite2)
e1y=sprite height(sprite1)
e2y=sprite height(sprite2)-( sprite y(sprite2)+sprite height(sprite2)-( sprite y(sprite1)+sprite height(sprite1) ) )
endif
if sprite y(sprite2)+sprite height(sprite2)<sprite y(sprite1)+sprite height(sprite1)
e2y=sprite height(sprite2)
e1y=sprite height(sprite1)-( sprite y(sprite1)+sprite height(sprite1)-( sprite y(sprite2)+sprite height(sprite2) ) )
endif
//--start at 12--rgbb,rgbg,rgbr,rgba
savex1=s1x
savex2=s2x
for s1y=s1y to e1y-1
s1x=savex1
s2x=savex2
for s1x=s1x to e1x-1
if memblock byte(memnum, 12+(s1y*image width(sprite image(sprite1))+s1x)*4)>0 || memblock byte(memnum, 12+1+(s1y*image width(sprite image(sprite1))+s1x)*4)>0 || memblock byte(memnum, 12+2+(s1y*image width(sprite image(sprite1))+s1x)*4)>0
if memblock byte(memnum2, 12+(s2y*image width(sprite image(sprite2))+s2x)*4)>0 || memblock byte(memnum2, 12+1+(s2y*image width(sprite image(sprite2))+s2x)*4)>0 || memblock byte(memnum2, 12+2+(s2y*image width(sprite image(sprite2))+s2x)*4)>0
collision=1
endif
endif
s2x=s2x+1
next s1x
s2y=s2y+1
next s1y
if memblock exist(memnum)=1
delete memblock memnum
endif
if memblock exist(memnum2)=1
delete memblock memnum2
endif
endif
endfunction collision
function TownHall()
if keystate(34) = 1 and thallexist = 0
global thallplace
thallplace = 1
endif
if thallplace = 1
sprite 2,x vector2(3)-15,y vector2(3)-11,4
global thallcollision
thallcollision = pixelcol(2,1)
if thallcollision > 0 and coordX# >= 4 and mouseclick() = 1
print "Invalid Location"
cls
thallplace = 0
delete sprite 2
endif
if mouseclick() = 1 and thallcollision = 0
thallplace = 0
experience = experience + 100
global thallx
global thally
thallx = x vector2(3)-15
thally = y vector2(3)-11
sprite 2,thallx,thally,4
thallexist = 1
thgridx = snapX
thgridy = snapY
CityArray(thgridx,thgridy) = 2
open to write 1,"files\private\TownHall.xy"
write string 1,str$(thallx)
write string 1,str$(thally)
write string 1,str$(thgridx)
write string 1,str$(thgridy)
close file 1
delete file "files\setup.txt"
open to write 1,"files\setup.txt"
write string 1,str$(experience)
write string 1,str$(level)
write string 1,str$(money)
write string 1,mname$
write string 1,cname$
close file 1
endif
endif
endfunction
Made with the latest DBP update if that matters.
Thanks!

Zeus