Hey guys, me and my friend are trying to make a simple shooter game (not tiles … dont go there.)
We have a small problem with and if statement placed within an enterframe clipevent, which basically tests to see if the weapon the guy is holding has changed or not, and if it has, then to update the display with the new weapon and new hand positions for the gun. Problem is, the code works outside the if statement but not within it…
Tracing the variable from within the if statement returns undefined, but the same trace command given outside the if statement returns the appropriate value!
onClipEvent (enterFrame) {
//set the direction of the human
if (_parent.LorR == 1) {
_parent._xscale = 100;
} else if (_parent.LorR == 0) {
_parent._xscale = -100;
} else {
//do nothing
}
//hold the weapon and switch if necessary
//create new weapon where necessary
if (_parent.newWeapon) {
removeMovieClip(_parent[_parent.whichWeapon]);
_parent.attachMovie(_parent.whichWeapon, _parent.whichWeapon, _parent.weaponDepth);
}
// PROBLEM CODE STARTS HERE!!!!!!!
trace(_parent[_parent.whichWeapon].rightHandPoint[0]); //THE PROBLEM
_parent[_parent.whichWeapon].rightHandPoint[1];
_parent[_parent.whichWeapon].leftHandPoint[0] = _parent[_parent.whichWeapon].leftHandPoint[0]*(_parent[_parent.whichWeapon].sizeScale/100);
_parent[_parent.whichWeapon].leftHandPoint[1] = _parent[_parent.whichWeapon].leftHandPoint[1]*(_parent[_parent.whichWeapon].sizeScale/100);
_parent[_parent.whichWeapon].rightHandPoint[0] = _parent[_parent.whichWeapon].rightHandPoint[0]*(_parent[_parent.whichWeapon].sizeScale/100);
_parent[_parent.whichWeapon].rightHandPoint[1] = _parent[_parent.whichWeapon].rightHandPoint[1]*(_parent[_parent.whichWeapon].sizeScale/100);
_parent.newWeapon = false;
}
trace(_parent[_parent.whichWeapon].rightHandPoint[0]); //SAME STATEMENT RETURNS CORRECT VALUE
//set the x position of the weapon
_parent[_parent.whichWeapon]._x = _parent["body_mc"]._x+(_parent[_parent.whichWeapon].distanceFromChest*10);
//set the y position of the weapon
_parent[_parent.whichWeapon]._y = -((_parent[_parent.whichWeapon].heightToHold)*10);
//set the scale of the weapon
_parent[_parent.whichWeapon]._yscale = _parent[_parent.whichWeapon].sizeScale;
_parent[_parent.whichWeapon]._xscale = _parent[_parent.whichWeapon].sizeScale;
//find the distance between the anchor points
//find the difference in X
//trace(_parent["body_mc"]._y);
this.anchorX = (_parent[_parent.whichWeapon]._x+_parent[_parent.whichWeapon].leftHandPoint[0])-_parent["upperLeft"]._x;
//find the difference in Y
this.anchorY = (_parent[_parent.whichWeapon]._y+_parent[_parent.whichWeapon].leftHandPoint[1])-_parent["upperLeft"]._y;
//find the total distance
this.anchorD = Math.sqrt(Math.pow(this.anchorX, 2)+Math.pow(this.anchorY, 2));
//find angle between the gun and the shoulder
this.anchorAngle = -(Math.atan(this.anchorY/this.anchorX)*180/Math.PI);
this.rotationAngle = Math.acos(anchorD/(_parent["lowerLeft"].armLength+_parent["upperLeft"].armLength))*180/Math.PI;
//position the upper arm
_parent["upperLeft"]._rotation = (this.rotationAngle-this.anchorAngle);
//position the lower arm
_parent["lowerLeft"]._x = _parent["upperLeft"]._x+_parent["upperLeft"]._width;
_parent["lowerLeft"]._y = _parent["upperLeft"]._y+_parent["upperLeft"]._height;
//rotate the lower arm
_parent["lowerLeft"]._rotation = -(this.rotationAngle-this.anchorAngle);
//same for the right hand
//find the distance between the anchorR points
//find the difference in X
this.anchorRX = (_parent[_parent.whichWeapon]._x+_parent[_parent.whichWeapon].rightHandPoint[0])-_parent["upperRight"]._x;
//find the difference in Y
this.anchorRY = (_parent[_parent.whichWeapon]._y+_parent[_parent.whichWeapon].rightHandPoint[1])-_parent["upperRight"]._y;
//find the total distance
this.anchorRD = Math.sqrt(Math.pow(this.anchorRX, 2)+Math.pow(this.anchorRY, 2));
//find angle between the gun and the shoulder
this.anchorRAngle = -(Math.atan(this.anchorRY/this.anchorRX)*180/Math.PI);
this.rotationRAngle = Math.acos(anchorRD/(_parent["lowerRight"].armLength+_parent["upperRight"].armLength))*180/Math.PI;
//position the upper arm
_parent["upperRight"]._rotation = (this.rotationRAngle-this.anchorRAngle+180);
//position the lower arm
_parent["lowerRight"]._x = _parent["upperRight"]._x+_parent["upperRight"]._width;
_parent["lowerRight"]._y = _parent["upperRight"]._y+_parent["upperRight"]._height;
//rotate the lower arm
_parent["lowerRight"]._rotation = -(this.rotationRAngle-this.anchorRAngle+180);
//_parent[_parent.whichWeapon]._x += 200;
}