0

I have recently started using LimeJS for my game development, and I have run into a bit of a hault.

I am making a RTS type of game. I have functions which let you chose buildings, and then click on the map to add chosen building to the map. I do not want the player to be able to place a building if it overlaps with one that is already there. How would I check this? The buildings are Sprites which in turn are added as children to a map sprite. I was thinking of using hitTest but I can't quite figure out how that function works. Example of the event handler:

goog.events.listen(field,['mousedown'],function build(e){


    if(selected_ == 1){
     var house = new rh.house().setPosition(e.position.x, e.position.y);
     field.appendChild(house);
     selected_ = -1;
     houselbl.setFill('assets/storage.png');
    }  
    if(selected_ == 2){
     var blacksmith = new rh.blacksmith().setPosition(e.position.x, e.position.y);
     field.appendChild(blacksmith);
     selected_ = -1;
     blacksmithlbl.setFill('assets/blacksmith.png');
    }      
    if(selected_ == 3){
     var lumbermill = new rh.lumbermill().setPosition(e.position.x, e.position.y);
     field.appendChild(lumbermill);
     selected_ = -1;
     lumbermilllbl.setFill('assets/lumbermill.png');
   }  
});

2 Answers 2

1

You might also want to put that collision check in a scheduleManager, it can then check it frame by frame and you could turn the sprite or building red if it does overlap.

0

I got it working. I used getBoundingBox on all of the children, and checked if it was safe. Example below:

goog.events.listen(field,['mousedown'],function build(e){ 

    if(selected_ == 1){
        var house = new rh.house().setPosition(e.position.x, e.position.y);
        var safe=true;
        for (var i=0;i<field.getNumberOfChildren();i++)
        { 
           var box = field.getChildAt(i).getBoundingBox();
           if (goog.math.Box.intersects(box,house.getBoundingBox())){
           safe=false;
       }
    }
        if (safe){
            field.appendChild(house);
            selected_ = -1;
            houselbl.setFill('assets/storage.png');
        }
    }  
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.