1

Hey Everyone so I am currently working on a game and the objective is for the user to click on objects on the stage. But the user has to click on the largest objects first before the user clicks on the smaller objects. I wanted to make it that if the user clicks on a smaller object first and not the larger one then the game will be over. I thought I could go about setting this up with booleans for each object on the stage but my if statements arent cutting it here is how I have it set up:

Here are my objects and booleans I use:

//Add box references
    public var box_1:MovieClip;
    public var box_2:MovieClip;
    public var box_3:MovieClip;
    public var box_4:MovieClip;

    //Booleans
    private var b1:Boolean;
    private var b2:Boolean;
    private var b3:Boolean;
    private var b4:Boolean;

now I set all the Booleans to false and if the user clicks on one of the objects I set the Boolean to true.

Here are my if statements tied to my main ENTER_FRAME Listener:

    private function level_1():void 
    {

        if (b1)
        {
            mainScreen.box_1.gotoAndPlay("B1GONE");
            b1 = false;
        }else
        if (b2)
        {
            mainScreen.box_2.gotoAndPlay("B2GONE");
            b2 = false;
        }else
        if (b3)
        {
            mainScreen.box_3.gotoAndPlay("B3GONE");
            b3 = false;
        }else
        if (b2 && !b1)
        {
            endGameCondition();
        }

    }

On this statement:

if (b2 && !b1)
        {
            endGameCondition();
        }

I was trying to state that if box_2 is true meaning that its clicked on and box_1 hasnt been clicked on yet which is the larger object then the game is now over due to the user not clicking on the largest object first. I have it setup to where box_1 is the largest object and the others are the next size down.

Can anyone see why this isnt working correctly or if there is a better method in doing this?

**** UPDATE HOW MY MAIN CLASS IS SETUP NOW **************

Where I add all my Movie clips and variables:

public class boxTapEngine extends MovieClip 
{

    //Screens
    private var mainScreen:mcMainScreen;



    //Add box references
    public var box_1:MovieClip;
    public var box_2:MovieClip;
    public var box_3:MovieClip;
    public var box_4:MovieClip;


    private var aBoxesArray:Array;

in my constructor function:

         aBoxesArray = [box_1, box_2, box_3, box_4];

        //AddMainScreen
        mainScreen = new mcMainScreen();
        mainScreen.x = (stage.stageWidth / 2);
        mainScreen.y = (stage.stageHeight / 2);
        stage.addChild(mainScreen);

        //Initiate numbers
        nLevel = 1;

        for each(var box:MovieClip in aBoxesArray)
        {
            box.addEventListener(MouseEvent.CLICK, boxClick);
        }

finally on the boxClick function:

private function boxClick(e:MouseEvent):void 
    {

         var box:MovieClip = e.currentTarget as MovieClip; //get a reference to one that was just clicked
         box.mouseEnabled = false; //we'll use this as a flag to know if it's been clicked yet
         box.gotoAndPlay("BGONE");


          //check to see if previous boxes have been clicked.
         //this will iterate through all the boxes in order
         for (var i:int = 0; i < aBoxesArray.length; i++)
         {
             if(aBoxesArray[i] == box) return; //if we've reached the currently clicked box, all is good, no need to keep checking so let's exit this loop and function
             if (!aBoxesArray[i].mouseEnabled)
             { //one of the previous boxes hasn't been clicked yet
                 endGameCondition();
             }
         }

    }
Nathan
  • 536
  • 4
  • 21

2 Answers2

1

Probably isn't working because your final else if statement won't ever be reached (because you're handling b2 == true earlier on which will then bypass all other else statements). Plus your setting b2 to false when you handle it earlier, so it will always be false by the time it gets your final statement.

You need to move that final else if before you check for the other things. See code comments:

    //Do this first, and not as a else if
    if (b2 && !b1){
        endGameCondition();
        return; //no need to check checking things if it's game over
    }

    //now you can do the rest
    if (b1)
    {
        mainScreen.box_1.gotoAndPlay("B1GONE");
        b1 = false;
    }else
    if (b2)
    {
        mainScreen.box_2.gotoAndPlay("B2GONE");
        b2 = false;
    }else
    if (b3)
    {
        mainScreen.box_3.gotoAndPlay("B3GONE");
        b3 = false;
    }

As an aside, you don't need the enter frame handler, you can just check everything on click. Something like this would work:

var boxes:Array = [box_1,box_2,box_3,box_4]; //make sure this is in order of what needs to be clicked first

//if you wanted to actually sort them dynamically based off total size (regardless of what order you've stuffed them in the array), you could add in something like this, which will order them from biggest to smallest:
boxes.sort(function(a,b){
    //compare which item has a greater total area (width * height)
    if(a.width * a.height > b.width * b.height) return -1; //A is bigger, so put a before b in the array
    if(a.width * a.height < b.width * b.height) return 1; //put b before a in the array
    return 0; //return 0 if they are the same
});




for each(var box:MovieClip in boxes){
    box.addEventListener(MouseEvent.CLICK, boxClick,false,0,true);
}

function boxClick(e:Event):void {
    var box:MovieClip = e.currentTarget as MovieClip; //get a reference to one that was just clicked

    box.mouseEnabled = false; //we'll use this as a flag to know if it's been clicked yet
    box.gotoAndPlay("BGONE");

    //or you could just do this to get rid of the box:
    if(box.parent) box.parent.removeChild(box);

    //check to see if previous boxes have been clicked.
    //this will iterate through all the boxes in order
    for(var i:int=0;i<boxes.length;i++){
        if(boxes[i] == box) return; //if we've reached the currently clicked box, all is good, no need to keep checking so let's exit this loop and function
        if(!boxes[i].mouseEnabled){ //one of the previous boxes hasn't been clicked yet
            endGameCondition();
        }
    }
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Hey @LDMS thanks for the response. For some reason the if statement still is being called :/. I was going to implement the second method that you wrote out but It wouldnt take into consideration that the user has to always click on the bigger box first ie box_1 before the user clicks on any other smaller boxes. – Nathan Apr 14 '15 at 07:02
  • Sure it does. As long as your array at the top is in order of biggest to smallest – BadFeelingAboutThis Apr 14 '15 at 15:57
  • Thank you So much for the detailed answer. I just accepted your answer. There is only one error that I am getting "Cannot access a property or method of a null object reference." I believe it has to do with the boxes not being available. I have it setup to where the boxes are already added to the stage well more specifically another Movie clip called "mainScreen" then from that screen I just reference the boxes as my code shows "box_1, etc..." Is there something I should be doing differently to avoid this error? Thanks again – Nathan Apr 15 '15 at 23:16
  • Wait maybe it doesnt have to do with that after all. I believe it has to do with how i setup the Array. I'm using flash develop so where it "extends movieclip" I add: private var aBoxesArray:Array; Then in my constructor function I initiate it like so: aBoxesArray = [box_1, box_2, box_3, box_4]; Is this the correct way of doing it? – Nathan Apr 15 '15 at 23:20
  • That's a perfectly acceptable way yes, as long as all those boxes exist at that point. If you are creating the boxes through code, it would be even better to just do it in a loop. If added a pastebin link or updated the question to include your entire class, I could comment on it better – BadFeelingAboutThis Apr 15 '15 at 23:26
  • Okay Just updated the question On the bottom I added how I have all my movie clips and variables setup as well as how I implemented your code thank you for doing this! – Nathan Apr 15 '15 at 23:34
  • How are you boxes instantiated? Are you using FlasPro timeline? – BadFeelingAboutThis Apr 15 '15 at 23:39
  • Yes I am. I have the boxes placed on my Movie clip "mainScreen" timeline the first frame. Thats where I gave them their instance names of box_1, etc... – Nathan Apr 15 '15 at 23:46
  • Wait I finally figured it out I forgot I had to reference it like so mainScreen.box_1. Thank you again for all your help! – Nathan Apr 15 '15 at 23:50
  • Oh Ok, so just prefix them with `mainScreen` then. eg. `mainScreen.box_1` . your `mainScreen` var though will have to be public I think or you may get an error about it clashing with your FlashPro instance name – BadFeelingAboutThis Apr 15 '15 at 23:50
  • Yeah i just figured that out haha. I am an idiot i did that before but i was getting an error because initiated the array then added the main screen to stage when i should have done the opposite thanks LDMS your the best man. – Nathan Apr 15 '15 at 23:51
  • Glad you got it all going! Good luck with the rest of your app – BadFeelingAboutThis Apr 15 '15 at 23:52
1
//Store your clips in an array
var myClips:Array = [mc1,mc2,mc3,mc4];

//get the clip sizes and store it to an array.
var sizes:Array = new Array();
for (var i:uint = 0; i < myClips.length; i++) {
    sizes.push(myClips[i].width);
}

//apply Numeric array sort. 
sizes.sort(Array.NUMERIC);
function onClickAction(e:MouseEvent):void { 
    //Check wheather the array is empty or not.
    if (sizes.length != 0) {
        //Check wheather the clicked object bigger or not.
        if (e.target.width == sizes[sizes.length - 1]) {
            trace("Bigger");
            e.target.alpha = .5;
            sizes.splice(sizes.length-1,1);         
        } else {
            trace("Smaller");
        }
    }
}
Benny
  • 2,250
  • 4
  • 26
  • 39
  • Awesome thanks so much for this comment. Im going to try and implement it right now and see if everything works out ill come back and let you know how it goes thanks @Benny – Nathan Apr 14 '15 at 07:04
  • Hey Benny the only problem I seem to be having is when I add this '.width;' to ' aSizesArray.push(aBoxesArray[i].width);' I get this error Cannot access a property or method of a null object reference. – Nathan Apr 14 '15 at 07:22
  • I uploaded a picture above showing how im using your method with Flash Develop – Nathan Apr 14 '15 at 07:24