[SOLVED] Deciding Between Overlapping Actors

Beaumint

  • Posts: 16
Hello stencyl community!  I had a quick question and was hoping you guys could help me.

I'm working on a tower defense game.  After creating initial towers I would like to be able to grant the option to upgrade them.  In order to do so, I would like to make it so that the player can click on the towers.

My problem is that right now there are times when the top of a tower and the bottom of a tower overlap one another.  Here is a picture.


Ideally, it would be great if I could set up a 'when the mouse is released' command and it would be able to decide between the two based on the y factor of the mouse which one to actually select... but I have no idea how to I would be able to make it compare the two towers and their y value.

Think you guys could help me out? xD  /beg  I've been wracking my brain for a long time now, haha.

« Last Edit: July 15, 2013, 08:52:24 am by Beaumint »

froz

  • Posts: 250
You could register the click in the scene behaviour (not the tower actor behaviour).

Something like this

Code: [Select]
if mouse was pressed
   {set y to scene-height
     for each actor of type towers
      {if mouse was pressed on actor
         {if y-position of actor > y
            {set y to y-position of actor
              set clickedActor to actor
             }
          }
       } trigger event for clickedActor (to open menu or whatever you want)
   }
What this should do:
1) work only when mouse was pressed
2) check each actor to see if mouse was pressed on the actor. If it was, check if this actor y the lower then any other clicked actor (as you first compare to screen height and later to any other clicked actor)



Another, maybe easier way this could be done is by creating additional, square, invisible actor (in create event for tower actor behaviour) and checking if it was clicked. This square actor would need to have physics disabled (as I guess you also disable physics for tower actors). The downside of this method is that the player would always need to click on that square at the bottom of the tower, while the first method would allow to register also clicks on the top of the tower, if there is no tower on the higher tile.

I guess yet another way would be by creating a region and checking if mouse is inside that region, but I haven't really tried that.

Beaumint

  • Posts: 16
Wow thank you so much for a quick reply, that makes so much sense.

Going to try to implement it now!

(I'm no programmer so implementing this sort of stuff sometimes takes me awhile, haha!)

Beaumint

  • Posts: 16
It worked!!!

Only one small problem though... whenever I run the game now and press ` to see the log I get the following error.  Error #1009  Cannot access a property or method of a null object reference.

I looked up solutions on the forums and found this solution:
http://community.stencyl.com/index.php/topic,10028.0/topicseen.html
Unfortunately my global.workdir was already written correctly so it seems like this solution does not apply to me.

Here is another screenshot, the error is really long. 


Like I said, my game runs fine with no fps issues or anything, just a long written out error in the log.  Should I just ignore it...? 

froz

  • Posts: 250
You definitely should not ignore this. I can't say why exactly it is happening, but probably somewhere in your code you are trying to access an actor which is not existing.

You can read about problems like this here:

http://www.thestencylblog.com/2012/06/23/debug-console-stencyl/

« Last Edit: November 16, 2020, 11:15:40 pm by Justin »

Beaumint

  • Posts: 16
Yeah I thought that too, but my game is getting to a pretty decent size in terms of actors and behaviors and after doing a quick scan I couldn't find anything out of place...

Is there any way to pinpoint it?
I just read the guide you linked and doesn't seem to have a 'problem is here' feature, but I could be wrong...

Beaumint

  • Posts: 16
Wow how bizarre. 

So instead of going through everything LINE BY LINE I decided to instead just turn off entire behaviors at a time and see if that solved it.

I found the behavior that was causing the problem, it was the one I had just created!

if mouse was pressed
   {set y to scene-height
     for each actor of type towers
      {if mouse was pressed on actor
         {if y-position of actor > y
            {set y to y-position of actor
              set clickedActor to actor
             }
          }
       } trigger event for clickedActor (to open menu or whatever you want)
   }

So I sat and I thought for awhile and decided to just re-enable it and see what happened, and POW, the error is gone...

So TL;DR, I disabled and then enabled a behavior and the problem disappeared.

Thanks all for your help!