Best way to stop sprites from merging?
BlitzMax Forums/BlitzMax Beginners Area/Best way to stop sprites from merging?
| ||
I have a sprite spawn rate, then I guess what is a typical move towards player function (e.g. if enemy.x < player.x etc) However once a number have appeared on the screen they begin to occupy the same space. What is the best way of preventing this (for a total beginner!) Im guessing at a sort of start of if imagescollide(image, image.x, image.y, 0, image, image.x, image.y 0) but then i am unsure how to approach what goes next? Any help would be most appreciated Tom |
| ||
You can either wait till there's nothing in it's spawn place, or find a new place. a) Don't spawn the next one until the first one is clear of the spawn point. Optionally have more spawn points for faster spawning since this limits the possible spawn rate. b) If the spawn point is occupied, spawn at a location relative to it, i.e. 1 sprite width to the left/right/top/bottom, etc. Bear in mind this requires free space around the spawn points for them to spawn into. Bear in mind if they all move at the same rate towards the same destination they're largely going to move as a big mass unless there are objects etc. that break up their path... this could be good or bad depending on what you want :0) |
| ||
Sorry I should have mentioned that the sprite spawn pointa are random so they already kind of spawn away from each other - the problem is that once there are a few on the screen and the player has moved around a bit, the sprites occupy the same single space - I was wondering if there was a way to prevent this from happening. |
| ||
regarding spawning, if they spawn from multiple locations just check the location before you spawn something to see if there's already something in the way. To keep them from walking over each other you will want to do some form of collision checking. Bounding box may be sufficient since you might not need them pixel perfect, but if you want things tighter then imagescollide is what you want. See if the sprite image will collide where you would like to move it to, if it does then you don't move it. If it doesn't then you update it's position. |
| ||
Thanks mate Ill have look at what I can cobble together and see what happens!! |
| ||
I'm a beginner myself, but you could try something like giving each object a circular radius, then check to see if they're too close. I just threw this together as a challenge for myself - press the left mouse button to spawn enemy units Graphics 400,400 HideMouse() Global GameObjectList:TList = CreateList() Local player:TPlayer = TPlayer.Create() Repeat Cls For obj:TGameObject = EachIn GameObjectList obj.updatestate() obj.drawself() obj.checkcollide() Next Flip Until KeyHit(KEY_ESCAPE) Or AppTerminate() ' * TYPES * Type TGameObject Field x:Float 'x coord Field y:Float 'y coord Field r:Int 'collision radius Method updatestate() Abstract Method drawself() Abstract Method checkcollide() For obj:TGameObject = EachIn GameObjectList If obj<>Self If Sqr((obj.x-x)^2+(obj.y-y)^2)<obj.r+r x:-Sin(ATan2(obj.x-x,obj.y-y))*2 y:-Cos(ATan2(obj.x-x,obj.y-y))*2 End If End If Next End Method End Type Type TPlayer Extends TGameObject Function Create:TPlayer() Local player:TPlayer=New TPlayer player.r=10 ListAddLast GameObjectList,player Return player End Function Method updatestate() x=MouseX() y=MouseY() If MouseHit(1) enemy:TEnemy = TEnemy.Create(x,y,Self) End Method Method drawself() SetColor 0,255,0 DrawOval x-r,y-r,r*2,r*2 End Method End Type Type TEnemy Extends TGameObject Field target:TGameObject Field speed:Int=2 Function Create:TEnemy(x:Int,y:Int,target:TGameObject) Local enemy:TEnemy=New TEnemy enemy.r=Rand(5,25) enemy.x=x enemy.y=y enemy.target=target ListAddLast GameObjectList,enemy Return enemy End Function Method updatestate() If target x:+Sin(ATan2(target.x-x,target.y-y))*speed y:+Cos(ATan2(target.x-x,target.y-y))*speed End If End Method Method drawself() SetColor 255,0,0 DrawOval x-r,y-r,r*2,r*2 End Method End Type |
| ||
WOW that is awesome, and works perfectly. I will have to have a good solid look through the code now to get my head around it but Im sure I will get there. If I could reach through screen and hug you.....I would |