hitesting the weapons - actionscript-3

i have a movieclip on my stage called dude inside the moviclip is a frame with a movie clip called axeframe with yet another movie clip called axe. what i want to do is make a hittest in the axeframe a so that when the axe (only axe not the character) hits an enemy(named enemy) on the stage he will disappear. this is my code:
addEventListener(Event.ENTER_FRAME, axehit);
function axehit(event:Event):void {
if (axe.hitTestObject(enemy)) {
removeChild(enemy.stage)
}
}
it gives me this error
1120: Access of undefined property enemy.if (axe.hitTestObject(enemy)) {
1120: Access of undefined property enemy.removeChild(enemy.stage)

You can't just reference to enemy without any further specification (it will assume enemy is a child of the movieclip in which you placed the code. Try stage.enemy instead, and this, or this.parent instead of axe.
(Assuming enemy is a movieclip on the stage, and the code you posted is inside axe)
Also, you should change removeChild(enemy.stage) to stage.removeChild(stage.enemy), and you should probably look into variable scopes.
Edit: Nope.
Sorry for that, just pretend you didn't read that (Forgot you can't just reference objects through Stage)
To be completely honest with you, this is the way I started as well but it's not the proper approach to flash coding. Firstly, you should try to keep all your code on the main timeline, and not in separate movieclips, in order for it to work together better. Once you get the hang of it, you should check out Object Oriented programming as well. It really increases the workflow and enables you to create bigger and way more complicated scripts.
more edits:
So to put this on the main timeline, would require something such as:
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void {
if (axe.hitTestObject(enemy)) {
this.removeChild(enemy)
}
}
Which is cleaner and more readable (and easier to find) as well. (Assuming axe and enemy are movieclips on the main stage)

Related

AS3 Accessing stage objects from a class

I'm writing a class Player, and I'm developing collisions but flash gives me an error to this line:
function checkCollision(event:Event)
{
if (this.hitTestObject(stage.wall)) // THIS LINE!!!!!!!!
{
trace("HIT");
}
else
{
trace("MISS");
}
}
}
The error is:
Access of possibly undefined property wall through a reference with
static type flash.display:Stage.
How can I access to wall ? wall is a symbol on the stage... Should I develop it in another way? please help
MovieClip is a dynamic object, whereas Sprite or Stage are not. With these classes, unless the property is explicitly defined, the compiler will complain about the absence of the property when you try to reference it.
If you're programming with Flash IDE, "Automatically Declare stage Instances" will create properties on your stage that make dot.notation pathing possible.
If you're creating objects dynamically, you'll have to either create the properties yourself (which is impossible with static classes like Sprite), or reference them by DisplayList fetching methods getChildAt() or getChildByName().
In the case of a class, unless you extend a class that is already a DisplayObject, you won't inherently have access to stage or root. You'd have to pass a reference to the MainTimeline or Stage manually (probably during instantiation). Even if you did extend a DisplayObject, you'd have to first parent the object to the stage; until then, the properties are null.
For the sake of argument, let's assume Player class is extending Sprite, and you have parented it to the stage. Your code would correctly be written as follows:
function checkCollision(e:Event) {
if (this.hitTestObject(this.root.getChildByName("wall"))) {
trace("HIT");
} else {
trace("MISS");
}
}
Notice that the call to "wall" is not on the Stage. That's because there is only one child of stage, and that's MainTimeline (a.k.a. root).
BTW, you had an extra close brace in you example.
Yep if you have automatically declare stage instances unchecked you will get that error. It is a good practice to declare everything in AS3 and not rely on the GUI to do it. Even if it is
Public var boringBackground:Sprite;
It will pay off in the end performance and coding wise.

accessing functions between two (2) movieclip inside a nested movieclip AS3

ok im a noob for AS3 since i just started, i have two (2) movieclips inside a movieclip, the main mc is called main_mc then the two movieclips inside named trigger_mc and move_mc, trigger_mc has the instance name of start_ani, then inside the timeline of main_mc i have this code:
import flash.events.MouseEvent;
start_ani.addEventListener(MouseEvent.CLICK, correctans);
function correctans(e:MouseEvent):void {
move_mc.animate();
}
then i created a motion as actionscript 3.0 using move_mc then i inserted the code inside the timeline of the move_mc itself, and i made a function for that motion called animate, my problem is how do i access a function between two movieclips which both are inside another movieclip, i know this method is not programming wise, but i kinda need to learn this, pls help me, i badly need this, thank you in advance.
Parent is a property, not a function - you don't need ():
this.parent.move_mc.animate();
Also, you didn't mention the instance name of the move_mc movieclip, but access like the above requires the instance name to be move_mc - the movieclip symbol name doesn't matter in actionscript.
Update 1: To clarify, you said: trigger_mc has the instance name of start_ani
Good, then this code will work:
start_ani.addEventListener(MouseEvent.CLICK, correctans);
But you didn't say: move_mc has the instance name of ???
So we don't know if this code will work:
function correctans(e:MouseEvent):void {
???.animate();
}
Fill in those ??? for one.
Update 2: do you know if the click handler is being fired? Why not add a trace statement?
function correctans(e:MouseEvent):void {
trace("got click event!");
???.animate();
}
Because for a CLICK event, you need:
start_ani.buttonMode = true;
Though as you say, this isn't good programming practice because this assumes those two movieclips are siblings of the same parent. It's not extensible. If they're not, your code could throw errors. Just keep that in mind.

In flash with as3.0, I have to call a function on the main stage from a movieClip

I have to call a function that is defined on the main stage of my project, and I have to call it from a MovieClip, what can I do?
I'm using flash cs5.5 and AS3.0
There are multiple ways to access the MainTimeline from objects on the stage. Probably the most reliable is 'root', but there is also 'parent' (but only if you MovieClip is a direct child of the main timeline).
// root should always work
Object(root).myFunc();
// parent will only work if your movieclip is a direct child of the main timeline
Object(parent).myFunc();
Note that you have to cast these are generic Objects (or MovieClip would work) because they return typed classes that don't have a 'myFunc' function in them.
You'll need this code on your main timeline:
function myFunc():void {
trace("My function got called!");
}
When I read your question it sounds as though you have a function defined in an action frame of your main timeline.
My answer may be out of reach for your current project, and ToddBFisher's answer is perfectly right. That said - I'm going to answer the question differently.
Instead of defining a function on the main timeline, set up a document class, define your functions there, and access the class's functions in your code. Keep as much code off your timelines as possible.
Downloadable files for Document Class example: http://www.isgoodstuff.com/2008/06/06/actionscript-30-documentclass-in-plain-english/
Setting up an AS3 class: http://www.adobe.com/devnet/flash/quickstart/creating_class_as3.html
Assuming your movie clip is a direct child of your main stage, in you movie clip you can do:
MovieClip(parent).theFunctionToCall();
if your MovieClip has a class, just add it to your main class using like:
var m:MovieClip = new MovieClip();
**addChild(m);
then you can get access into it's public function like typing:
m."functon name";

AS3 - gotoAndStop with immediate action

I'm moving from AS2 to AS3 and probably as many ppl before found this incompatibility:
I used quite often code like:
gotoAndStop(5);
trace(box); //where box is a movie on 5th frame
What is the easiest way how to do it in AS3.
There is an easy way to solve this, but it is undocumented:
addFrameScript(1, update);
gotoAndStop(2);
function update() {
trace(box); // outputs [object MovieClip]
}
Please note that the first argument for addFrameScript is the frame number but it's 0-based, i.e. 0 is frame 1, 1 is frame 2, etc... The second argument is the function you would like to call.
no easy way to do that.
what you need to do is
setup a listener for when the frame renders
tell it to go to the said frame(5)
force the rendering to happen ASAP stage.invalidate
.
One of the top reasons to stay with as2.
Not saying as2 is better, just better at a few things and this is one of them. My opinion on this is that as3 wasn't really meant to handle timelines very well.
with as2 you do
gotoAndStop(5);
trace(box);
With as3 you need to wait for the timeline to render.
stage.addEventListener(Event.RENDER, onRenderStage);
protected function onRenderStage(ev:Event):void {
trace(this['box']);
}
gotoAndStop(5);
stage.invalidate();
I used to have different assets in different frames of one MovieMlip in my as2 days, but to do that in AS3 is too complicated to enjoy any of the benefits. So while this will work, I'd recommend looking into a different solution altogether. Or stick to as2.

Switching flash scenes within a MovieClip

How do I switch scenes within a flash MovieClip object with Flash CS5 and ActionScript 3?
Hi Dani,
First of all, coding within MovieClip objects in AS3 is not recommended AND using scenes are not recommended.
Why?
Coding within MovieClip objects can be tempting if you are a beginner and this is alright.But if you are serious about the reusability of your assets and your code, you may want to dissociate your visual from your logic.I'm suggesting you write your logicFrom the main timelineFrom external classes (and using OOP)This is great!
Scenes are bad because of the timeline and code issues.If some of your code is in a scene, it may be not accessible to the other scenes.
Enough talking, here's the help you need.
How do I switch scenes within a Flash MovieClip object
This code is intended to be in a MovieClip frame
// === Let's put the stage in a variable (cleaner) ===
var main:MovieClip = this.parent as MovieClip;
// this.parent will return the DisplayObject of parent the current clip.
// You need to cast [... as MovieClip] to not cause errors because Flash
// thinks it is only a DisplayObject
// === Here's the interresting part ===
main.gotoAndPlay(0, "Scene 2");
// We tell the main timeline to go to frame 0 in the "Scene 2"
// Be cautious, it must be spelled exactly as displayed in Flash (IDE)
Don't forget: Deeper is your clip (Embeded multiple times in a clip), more "parent" will you need.
var main:MovieClip = this.parent.parent as MovieClip;
// If your object is inside a MovieClip who is itself in a MovieClip
// Tip: How much time you need to push the Back button to go to the timeline
// is the number of parents you need to write.
Hoping this help. If you have any questions, just comment this answer!