The code illustrates the problem pretty well, I’ve cut everything unrelated out.
Create 2-4 kinsmen when the game starts
function createInitialKinsmen()
{
for (var k:int = 1; k < 3 + Math.round(Math.random() * 2; k++)
{
if (k == 1)
{
createKinsmen(0);
trace ("player");
}
else
{
createKinsmen(1);
trace ("starting kinsmen");
}
}
}
Kinsmen creation function
function createKinsmen(arrivalTypeVar:int)
{
var newKinsmen = new kinsmen;
listPanel.kinsmenDivider.addChild(newKinsmen);
totalKinsmen++;
totalKinsmenAlive++;
newKinsmen.name = "kinsmen" + totalKinsmen;
newKinsmen.arrivalType = arrivalTypeVar;
}
Kinsmen class
public class kinsmen extends MovieClip
{
var arrivalType:int;
function kinsmen()
{
trace(this.name);
if (this.arrivalType = 0)
{
trace("player");
}
if (this.arrivalType = 1)
{
trace("starting kinsmen");
}
}
}
The output should say:
kinsmen1
player
player
kinsmen2
starting kinsmen
starting kinsmen
kinsmen3
starting kinsmen
starting kinsmen
kinsmen4
starting kinsmen
starting kinsmen
But instead says:
kinsmen1
player
player
kinsmen2
player
starting kinsmen
kinsmen3
player
starting kinsmen
kinsmen4
player
starting kinsmen
Which means that the arrivalType variable isn’t being passed on. It seems to be hard-coded variables can be passed though thus at the moment I have a square with alpha 0 inside the kinsmen movieclip and it’s x position determines the value of arrivalType in the constructor but this surely can’t be good practice, is there a better way of doing this?
I think you just want to modify your constructor function for the
kinsmenclass to accept a parameter for thearrivalTypeand thename.What you’re currently doing is constructing a new
kinsmenobject and afterwards setting thearrivalTypeand thenameon that object. So when the constructor code runs, thearrivalTypeis the default value of anintwhich is 0.Modify your constructor function like this in the
kinsmenclass:And modify your
createKinsmen()function so that it passes in a value for thearrivalTypeto the constructor method, instead of setting thearrivalTypeandnameafter the object is created: