Note that there are some explanatory texts on larger screens.

plurals
  1. POPositioning a movieclip onto another movieclip 2
    primarykey
    data
    text
    <p>I'm creating a game where if you hit a zombie the zombie dies and his head is sent back in the direction you hit. It is like playing baseball with the zombies head with the head being the ball. I have two movieclips the zombie and the zombie head. Once the zombie is hit it will play its dying animation and remove itself and at the same time the zombie head will be added to the x and y co-ordinates of the dying zombie and be blown back like playing baseball. I have done the code for the hittest and the zombie dying and respawning but I can't seem to position and add the head to the dying zombie when it is hit. I have done the function for the zombie head but how can I add it to the zombie. I thought it will be like this, I added this in the PlayDeathAnimation function in the zombie class but did not work:</p> <pre><code>for (var i=0; i &lt; MovieClip(parent).zombies.length; ++i) { var zh = new ZombieHead(); zh.x = MovieClip(parent).zombies[i].x; zh.y = MovieClip(parent).zombies[i].y; zh.rotation = MovieClip(parent).zombies[i].rotation; addChild(zh); } </code></pre> <p>I even tried just this. The head does spawn but just stays static and does not fly back and it removes itself when the zombie removes itself which I don't wan't. I have already told the ZombieHead when to remove itself in its own class</p> <pre><code>var zh = new ZombieHead(); addChild(zh); </code></pre> <p>This is what i done so far Player</p> <pre><code>package { import flash.display.MovieClip; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.ui.Keyboard; import flash.display.Graphics; import flash.utils.setTimeout; public class Player extends MovieClip { //Player Setting var walkSpeed:Number = 4; var walkRight:Boolean = false; var walkLeft:Boolean = false; var walkUp:Boolean = false; var walkDown:Boolean = false; var attacking:Boolean = false; var attackRange:int = 100; var attackAngle:int = 30; public function Player() { stage.addEventListener(KeyboardEvent.KEY_DOWN,walk); addEventListener(Event.ENTER_FRAME,Update); stage.addEventListener(KeyboardEvent.KEY_UP,stopWalk); stage.addEventListener(MouseEvent.CLICK,attack); // The lines below draw a preview of your attack area graphics.beginFill(0x00ff00, 0.2); graphics.lineTo(attackRange*Math.cos((rotation-attackAngle)/180*Math.PI),attackRange*Math.sin((rotation-attackAngle)/180*Math.PI)); graphics.lineTo(attackRange*Math.cos((rotation+attackAngle)/180*Math.PI),attackRange*Math.sin((rotation+attackAngle)/180*Math.PI)); graphics.endFill(); } function walk(event:KeyboardEvent) { //When Key is Down if (event.keyCode == 68) { walkRight = true; } if (event.keyCode == 87) { walkUp = true; } if (event.keyCode == 65) { walkLeft = true; } if (event.keyCode == 83) { walkDown = true; } } function Update(event:Event) { //if attacking is true then key moves are false; if ((attacking == true)) { walkRight = false; walkLeft = false; walkUp = false; walkDown = false; // see if the zombie is in the cone for (var i:int=MovieClip(parent).zombies.length-1; i&gt;=0; i--) { if (inAttackCone(MovieClip(parent).zombies[i])) { if (hitTestObject(MovieClip(parent).zombies[i])) { //attacking = true; MovieClip(parent).zombies[i].zombieDead = true; } } } } else if ((attacking == false)) { //Else if attacking is false then move and rotate to mouse; var dx = parent.mouseX - x; var dy = parent.mouseY - y; var angle = Math.atan2(dy,dx) / Math.PI * 180; rotation = angle; if ((walkRight == true)) { x += walkSpeed; gotoAndStop(2); } if ((walkUp == true)) { y -= walkSpeed; gotoAndStop(2); } if ((walkLeft == true)) { x -= walkSpeed; gotoAndStop(2); } if ((walkDown == true)) { y += walkSpeed; gotoAndStop(2); } } } //Calculate the distance between the two public function distanceBetween(player:MovieClip,zombies:MovieClip):Number { for (var i:int=MovieClip(parent).zombies.length-1; i&gt;=0; i--) { var dx:Number = x - MovieClip(parent).zombies[i].x; var dy:Number = y - MovieClip(parent).zombies[i].y; } return Math.sqrt(((dx * dx) + dy * dy)); } public function angleDifference(a:Object, b:Object):Number { var dx:Number = b.x - a.x; var dy:Number = b.y - a.y; var ang:Number = (a.rotation/180*Math.PI)-Math.atan2(dy, dx); while (ang&gt;Math.PI) { ang -= 2 * Math.PI; } while (ang&lt;-Math.PI) { ang += 2 * Math.PI; } return Math.abs(ang*180/Math.PI); } function inAttackCone(enemy:MovieClip):Boolean { var distance:Number = distanceBetween(this,enemy); distance -= enemy.width / 2;// account for the enemy's size if (distance &lt; attackRange) { // In range, check angle if (angleDifference(this,enemy)&lt;attackAngle) { return true; } } return false; } function stopWalk(event:KeyboardEvent) { if ((attacking == false)) { if (event.keyCode == 68) { event.keyCode = 0; walkRight = false; gotoAndStop(1); } if (event.keyCode == 87) { event.keyCode = 0; walkUp = false; gotoAndStop(1); } if (event.keyCode == 65) { event.keyCode = 0; walkLeft = false; gotoAndStop(1); } if (event.keyCode == 83) { event.keyCode = 0; walkDown = false; gotoAndStop(1); } } } function attack(event:MouseEvent) { if ((attacking == false)) { attacking = true; gotoAndStop(3); } } } } </code></pre> <p>Zombie</p> <pre><code>package { import flash.display.MovieClip; import flash.events.Event; import flash.geom.Point; public class Zombie extends MovieClip { var walkSpeed:Number = 2; var target:Point; public var zombieDead:Boolean = false; public function Zombie() { //set target location of Zombie target = new Point(Math.random() * 500,Math.random() * 500); } function loop() { if (zombieDead == true) { playDeathAnimation(); } else if (zombieDead == false) { gotoAndStop(1); //Point Zombie at its target var dx = MovieClip(root).Player01.x - x; var dy = MovieClip(root).Player01.y - y; var angle = Math.atan2(dy,dx) / Math.PI * 180; rotation = angle; //Move in the direction the zombie is facing x = x+Math.cos(rotation/180*Math.PI)*walkSpeed; y = y+Math.sin(rotation/180*Math.PI)*walkSpeed; //Calculate the distance to target var hyp = Math.sqrt((dx*dx)+(dy*dy)); if (hyp&lt;5) { target.x = Math.random() * 500; target.y = Math.random() * 500; } } } public function playDeathAnimation() { gotoAndStop(2); } public function deathAnimationFinishedCallback() { for (var i=0; i &lt; MovieClip(parent).zombies.length; ++i) { if (MovieClip(parent).zombies[i] == this) { MovieClip(parent).zombies.splice(i, 1); break; } } MovieClip(parent).removeChild(this); } } } </code></pre> <p>ZombieHead</p> <pre><code>package { import flash.display.MovieClip; import flash.events.Event; public class ZombieHead extends MovieClip { var headSpeed:Number = -30; var friction:Number = 0.9; public function ZombieHead() { // constructor code addEventListener(Event.ENTER_FRAME,Update); } function Update(event:Event) { x = x+Math.cos(rotation/180*Math.PI)*headSpeed; y = y+Math.sin(rotation/180*Math.PI)*headSpeed; headSpeed *= friction; if (headSpeed &gt; -0.00001) { removeEventListener(Event.ENTER_FRAME,Update); parent.removeChild(this); } else if (x&lt;0 || x &gt; 550 || y &lt; 0 || y &gt; 400) { removeEventListener(Event.ENTER_FRAME,Update); parent.removeChild(this); } } } } </code></pre> <p>Document Class</p> <pre><code>package { import flash.display.MovieClip; import flash.events.Event; public class Game extends MovieClip { public var zombies:Array; public function Game() { addEventListener(Event.ENTER_FRAME, update); zombies = new Array(); } public function update(e:Event) { //Only spawn a Zombie if there are less than number if (numChildren &lt; 4) { //Make a new instance of the Zombie. var Z = new Zombie(); addChild(Z); //Position and rotate the zombie Z.x = Math.random() * stage.stageWidth; Z.y = Math.random() * stage.stageHeight; Z.rotation = Math.random() * 360; zombies.push(Z); } for (var count = 0; count&lt;zombies.length; count ++) { zombies[count].loop(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload