1

Hey everyone so having a little trouble trying to accomplish this. I understand how to add lives and display them through a Dynamic Text Field on the stage. My game is set up right now to where this happens and whenever the player dies a live decrements so it works fine.

But I want to display and Image of all 3 lives a custom Movie Clip that I drew in Flash CS6 to represent the lives. So All 3 lives will be displayed in the game and once the player dies one of the lives images is removed.

I have some idea on what to do. For instance I created a "for loop" to display all 3 images on the screen and created variables to place them horizontally next to each other with a space of 30 pixels.

But I'm not sure if this is the correct way to approach this. Also kind of confused on how I would remove one of the images when the player dies?

Her is my code so far:

    public var playerLives:mcPlayerLives;
    private var nLives:Number = 3;
    private var startPoint:Point;
    private var aPlayerLivesArray:Array;

In my main class Engine:

aPlayerLivesArray = new Array;
addPlayerLivesToStage();

Then the addplayerlivestostage function:

public function addPlayerLivesToStage():void
    {

        var startPoint:Point = new Point((stage.stageWidth / 2) - 300, (stage.stageHeight / 2) - 200);
        var xSpacing:Number = 30;

        for (var i = 0; i < nLives; i++)
         {
             trace(aPlayerLivesArray.length);
             playerLives = new mcPlayerLives();
             stage.addChild(playerLives);
             playerLives.x = startPoint.x + (xSpacing * i);
             playerLives.y = startPoint.y;
             aPlayerLivesArray.push(playerLives);
         }
    }

So like i stated above everything works fine and it does display the 3 images that represent the lives, but would this be the correct approach or is there an easier method?

Nathan
  • 536
  • 4
  • 21

2 Answers2

1

i think you're using right way to add lives. and for removing them, you don't need to remove all and add new lives, you can remove the last element of lives array, so, it's done, i think thats already an easy method

so, you can implement this

for (var i = 0; i < nLives; i++)

to make a better usage for "adding lives in-game (earning lives)" something like

for (var i = aPlayerLivesArray.length; i < nLives; i++)

but don't forget to decrease array length by 1 after removing last element of livesArray when player dies

Bora Kasap
  • 392
  • 4
  • 12
  • Thanks for the help Guys. But still a little confused so in a separate function i have which is my gameloop called "checkPlayerHitShark" where it does a hittest to check if the player hit the shark. So when the playerHook does hit the shark i want the lives to decrement by 1 so like you said i know i need a nLives --; but not exactly sure how to use it with the for loop. Its not removing the image lives on the screen. It does decrement it though. should i make a separate function like stated above that removes a live then call on it in my "Checkplayerhitshark" function? – Nathan Feb 10 '14 at 22:26
  • alright just need to figure out how to remove the movie clip image lives from the screen. Ill try some things – Nathan Feb 10 '14 at 22:27
  • yeah, it's better you should use another function for removing lives. But you don't need to use another function to add lives in my implement. But if you want to use only 1 function for adding and removing, actually that's not "so" right but it is fine... You can decrease nLives by 1 then remove all elements of livesArray and set livesArray.length=0; then you can use your own function to add lives again, that makes it look like live is removed but actually all lives are removed and added again by new nLives value. – Bora Kasap Feb 10 '14 at 22:28
  • Well im just trying to remove lives. My game has no option to add lives. But Im still having a problem removing the playerLives image from screen. It removes one but still gives me a error. saying display must be a child of the caller etc... – Nathan Feb 11 '14 at 01:47
  • How would i go about decreasing the array by 1? i was thinking using splice(i,1)? – Nathan Feb 11 '14 at 04:44
  • Yeah sorry for that. I was just being impatient because i was trying to figure it out for like 2hrs. But i finally got it. thanks though – Nathan Feb 11 '14 at 19:12
1

Looks pretty close to a reasonable approach. I would have a blank container movieclip on stage where you want the lives icons to display. Create one life icon in your library and link it for export with actionscript. When you generate your game view, you can populate this container with the starting value of three lives. Place the first one, then place the subsequent ones based on the first location.

Some untested code:

NOTE: The following presumes your container clip for the lives is named lives_container and your link instance name for the life icon in the library is Life_icon.

var numberOfLives:Number = 3;
var iconSpacing:Number = 5;
var nextX:Number = 0;

for(var i:int = 0; i < numberOfLives; i++ )
{
    var icon:MovieClip = new Life_icon();
    icon.x = nextX;
    lives_container.addChild( icon );

    nextX += icon.width + iconSpacing;
}

This way you could add extra lives easily if the player gained any by adding new icons at the last nextX value, like so:

function addLife():void
{
    var icon:MovieClip = new Life_icon();
    icon.x = nextX;
    lives_container.addChild( icon );

    nextX += icon.width + iconSpacing;
}

or remove them as the player loses them:

function removeLife():void
{
    var numberOfLivesDisplayed:Number = lives_container.numChildren();
    lives_container.removeChildAt( numberOfLivesDisplayed - 1 );

    nextX -=  icon.width + iconSpacing;
}

Using a container clip for the lives icons makes adjusting the location of the life icons easier if it becomes necessary later.

Ribs
  • 1,449
  • 2
  • 13
  • 27
  • So i created a removePlayerLive() function and i call on that function inside my checkPlayerHitShark() function. so whenever the hittest happens I want that function to initiate. But i dont know exactly what to add in it to decrement the lives and remove one playerLives image from the screen. I have this in the remove function: for (var i = 0; i < nLives; i++){ nLives --; stage.removeChild(playerLives);} but gives my a error – Nathan Feb 11 '14 at 01:40
  • 1
    you really need to get off the stage. Per my example, you should put the lives into a container movieclip. Your stage has all kinds of stuff on it, which makes removing children of its display list problematic. Also, I can't understand why you are using a loop. the error 'display must be a child of the caller...' results from calling removeChild() for a movieclip that doesn't exist on stage. When your player dies, you only need to remove one life per death, not loop through them. Also note that the code I posted does not attempt to do anything other than manage the lives display. – Ribs Feb 11 '14 at 01:53