Alexis Kyle Sales
Alexis Kyle Sales

Reputation: 19

Movieclips Overlapping

I've been learning as3 for about 2 weeks

I'm trying to make a combo generator for personal uses

The problem is that if the same MovieClip inside the array is displayed twice or more it just overlapps it.

for example when the elbow is displayed a second time ,it just overlaps the 1st elbow movieclip! http://imageshack.us/scaled/landing/208/17797019.png

Here's my code

var punch:Punch = new Punch();
var kick:Kick = new Kick();
var knee:Knee = new Knee();
var elbow:Elbow = new Elbow();
var holder:MovieClip; // for displaying purposes 
var position:Number = 100;

public function Main()
{
    var combo:Array = [punch, kick, knee, elbow];
    for(var i:Number = 0;i < 2;i++ )
    {
        holder = combo[randomNumber()]
        holder.y = 200;
        holder.x = position;        
        addChild(holder);
        position = position + 100;
    }


}
function randomNumber():Number
{
    return(Math.floor(Math.random() * (4 - 1 )+1));
}

Upvotes: 0

Views: 251

Answers (3)

Joel
Joel

Reputation: 1630

I am assuming you wish for two "Elbow"s to appear at once? Well you only have one instance of the Elbow class, so that wont work.

I recommend that you have a function which takes in an integer, and returns the new instance of the appropriate MovieClip:

function NewMove(id:int):MovieClip
{
    switch(id)
    {
        case 1: return new Punch();
        case 2: return new Kick();
        case 3: return new Knee();
        case 4: return new Elbow();
    }
    return new MovieClip();
}

So your code would be:

for(var i:Number = 0;i < 2;i++ )
{
    holder = NewMove(randomNumber());
    holder.y = 200;
    holder.x = position;        
    addChild(holder);
    position = position + 100;
}

Upvotes: 0

Ivan Chernykh
Ivan Chernykh

Reputation: 42166

Try to change your code like this:

public function Main()
{
    var combo:Array = [Punch, Kick, Knee, Elbow];
    for(var i:Number = 0;i < 2;i++ )
    {
        var motion:MovieClip = new combo[randomNumber()]();           
        motion.y = 200;
        motion.x = i*100;        
        addChild(motion);
    }    
}
function randomNumber():Number
{
    return(Math.floor(Math.random() * (4 - 1 )+1));
}

Upvotes: 1

Dave
Dave

Reputation: 46259

I think your real issue is that you have objects in that array multiple times in the first place. I guess you want to add 2 elbows; a left and a right. That means you need 2 elbow objects (the way objects work is that you have a single class, many instances. The same instance cannot be in multiple places, because it has only one set of properties)

So you need to make (say) var elbowL:Elbow = new Elbow(); var elbowR:Elbow = new Elbow(); and add both to the array.

As for the overlapping, I believe an object can only be added to the stage once, so adding it a second time replaces the first. In this case that's making no difference though (except changing the z-order); the elbow is being moved as soon as you change it's x or y property. With the change I suggested above, that's no longer an issue.

Upvotes: 0

Related Questions