-
Help with hitting enemy
My Hero and Enemy are in the same movieclip.
The Hero adds a punch movieclip like this:
parent.addChild(_punch);
_punch.x = this.x;
_punch.y = this.y;
How do I detect the punch hitTest with the enemy?
Please help!
Desperate and replies are extremely apreciated!
-
HI,
You could use a simple hitTestObjec.
PHP Code:
stage.addEventListener(Event.ENTER_FRAME, doHitTest);
function doHitTest(e:Event):void
{
if (_punch.hitTestObject(otherThing))
{
trace("hit");
}
}
-
The Map Movieclip that contains both the Hero and the Enemy is where the _punch is being added.
That is my exact code.
The Enemy has the instance name of "Sheep"
How then, do I access the punch? Do I do this inside the punches Class File?
My main question is that I do not know how to access the punch.
Thank you so much!
-
Hi,
Can't do anything without your fla or lots more code and graphics
-
1 Attachment(s)
Here is everything that's required for analysis. Hope you can help !!
PHP Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Hero extends MovieClip {
private var _vx = 5;
private var _vy = 5;
private var _movingDown:Boolean = false;
private var _movingUp:Boolean = false;
private var _movingLeft:Boolean = false;
private var _movingRight:Boolean = false;
private var _fireQ:Boolean = false;
private var _sideToSide:SidetoSide;
public function Hero() {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
}
private function onAddedToStage(event:Event):void
{
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
_sideToSide = new SidetoSide;
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
removeEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
removeEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
}
private function onKeyPressed(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.W)
{
_movingUp = true;
}
if(event.keyCode == Keyboard.S)
{
_movingDown = true;
}
if(event.keyCode == Keyboard.A)
{
_movingLeft = true;
}
if(event.keyCode == Keyboard.D)
{
_movingRight = true;
}
if(event.keyCode == Keyboard.Q)
{
_fireQ = true;
}
}
private function onKeyReleased(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.W)
{
_movingUp = false;
}
if(event.keyCode == Keyboard.S)
{
_movingDown = false;
}
if(event.keyCode == Keyboard.A)
{
_movingLeft = false;
}
if(event.keyCode == Keyboard.D)
{
_movingRight = false;
}
if(event.keyCode == Keyboard.Q)
{
_fireQ = false;
}
}
private function enterFrameHandler(event:Event):void
{
//Hero moves left
if(_movingLeft && !_movingRight)
{
x -= _vx;
rotation = 270;
}
//Hero moves right
if(!_movingLeft && _movingRight)
{
x += _vx;
rotation = 90;
}
//Hero moves Up
if(_movingUp && !_movingDown)
{
y -= _vy;
rotation = 0;
}
//Hero moves Down
if(!_movingUp && _movingDown)
{
y += _vy;
rotation = 180;
}
if(_fireQ)
{
parent.addChild(_sideToSide);
_sideToSide.x = this.x;
_sideToSide.y = this.y;
_sideToSide.rotation = this.rotation;
}
}
}
}
PHP Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class SidetoSide extends MovieClip {
public function SidetoSide() {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onAddedToStage(event:Event):void
{
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
if(currentFrame == 11)
{
parent.removeChild(this);
}
}
}
}
If there's anything else you need let me know I will add it in
-
Hi,
PHP Code:
private function enterFrameHandler(event:Event):void
{
if(currentFrame == 11)
{
parent.removeChild(this);
}
if(this.hitTestObject(parent.parent.area.Sheep))
{
trace("Hit");
}
}
I gave the clip with the sheep and box inside the name of "area"
-
I am extremely grateful. Bye and enjoy your evening!