AS3. MovieClip over other movieclip instead of mouse_over. - actionscript-3

I have action
MC1.addEventListener (MouseEvent.MOUSE_OVER, MC1_over);
can a use other MC instead of Mouse?
In other words, when MC2 will be over MC1, my action will start. How do that?
Thanks for help

You'll have to check for intersection. It's called HitTesting and there are several ways to approach this. But first - it won't be an Event anymore, you'll have to check for an intersection in every frame. So first of all, we need to create a new Event.ENTER_FRAME listener.
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
//Your code will go here
}
Second, we check our objects for an intersection of their boudary rectangles. It's ok if you have sqare or rectangular movieclips, if your MCs are more complex (two circles for example) you'll have to use other ways of getting this intersection.
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
if(MC1.getRect(this).intersects(MC2.getRect(this)))
{
//Two movieclips are intersecting
}
}
Third, as long as this condition will be true as long as your MCs are intersecting, we need to define a flag that will tell us if we've already done something we wanted to do.
var alreadyHandled:Boolean = false;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
if(MC1.getRect(this).intersects(MC2.getRect(this)))
{
if(!alreadyHandled)
{
doSomething();
alreadyHandled = true;
}
}
else
{
//When our movieclips are apart again, we reset our helping variable
alreadyHandled = false;
}
}
function doSomething():void
{
//We do what we want to do if our MCs are intersecting
}
If you want to do something continiously, when your movieclips are intersecting, just ignore that helping flag thing.
And by the way, I suggest you to start names your variables with a lowercase letter. In AS3 only Classes and Interfaces have names that start with a capital letter.

Thank you.
Everything works great when I do this on new as3 file.
But i need to use this in class document
When i use
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
if(MC1.getRect(this).intersects(MC2.getRect(this)))
{
trace("intersects")
}
}
Erron#1034: cannot convert type global#23b3a0d1 on
flash.display.DisplayObject.
Perhaps You known where is problem?

Related

how I can arrange the object on the stage AS3

I have a puzzle and 9 pictures, so when I Start Drag any picture i want it to come above of the all remaining.
How I can do it on Action Script 3.0?
this is for example:
import flash.events.Event;
Puz3_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz3);
function StartPuz3 (e:Event):void
{
Puz3_Level7A.startDrag();
}
Puz2_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz2);
function StartPuz2 (e:Event):void
{
Puz2_Level7A.startDrag();
}
Puz1_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz1);
function StartPuz1 (e:Event):void
{
Puz1_Level7A.startDrag();
}
Try following.
import flash.events.Event;
Puz3_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz3);
function StartPuz3 (e:Event):void
{
this.setChildIndex(Puz3_Level7A, this.numChildren - 1);
Puz3_Level7A.startDrag();
}
Puz2_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz2);
function StartPuz2 (e:Event):void
{
this.setChildIndex(Puz2_Level7A, this.numChildren - 1);
Puz2_Level7A.startDrag();
}
Puz1_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz1);
function StartPuz1 (e:Event):void
{
this.setChildIndex(Puz1_Level7A, this.numChildren - 1);
Puz1_Level7A.startDrag();
}
This code assume all the above sprites are children of same sprite.
you can just do
this.parent.addChild(this);
to put 'this' on the top of all parent's children
There are some perfectly good answers already, but in the interest of DRY (don't repeat yourself) and completeness, here is my take on this.
A clean way of doing this, including stopping the drag. No inline functions or unnecessarily repeated code.
//add the mouse down listener to all objects you want to be able to drag.
//if you have a lot, you could also do this in a loop to make it more concise.
Puz3_Level7A.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
Puz2_Level7A.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
Puz1_Level7A.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
var currentPiece:Sprite; //a var to hold a reference to item last mouse downed (to use in the mouse up handler)
//the mouse down handler to drag a piece
function dragPuzzlePiece(e:Event):void
{
currentPiece = e.currentTarget as Sprite; //the event's current target is a reference to the item you added the listener to. We are casting it as a Sprite so the compiler knows what kind of object it is.
addChild(currentPiece); //this will bring it to the top.
//setChildIndex(currentPiece, numChildren-1); //this would do the same as above
//swapChildren(currentPiece, getChildAt(numChildren-1); //this would do the same as above
currentPiece.startDrag(); //start dragging the piece that was clicked
//now listen for the mouse up event to stop dragging the piece
//we listen on the stage, as sometimes dragging can lag when moving the mouse quickly and the mouse may not be over the item when you release the button
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
function mouseUpHandler(e:Event):void {
//remove the mouse up listener
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
//stop the drag:
if(currentPiece) currentPiece.stopDrag();
//do something with currentPiece now that the drag is done.
//like make sure the piece is in a valid spot
//check if the puzzle is complete now
puzzleDone(); //let's say you call this function at some point
}
function puzzleDone():void {
currentPiece = null; //clear this var so objects can be garbage collected
//!important
//when you use setChildIndex, or AddChild, or SwapChildren
//or anything that modifies the parentage of a object
//that object will no longer be removed by timeline keyframes
//it will stick around until it's parent is removed, or you explicitly remove it.
//to that end, you'll want to manually remove your puzzle pieces when done
removeChild(Puz3_Level7A);
removeChild(Puz2_Level7A);
removeChild(Puz1_Level7A);
//you can use some of patterns below for this as well.
}
In regards to adding the mouse down listeners to all your puzzle pieces, here are some other options to scale better if you have a whole lot of pieces:
If the pieces are all FlashPro instance names, you could do this:
//loop three times
for(var i:int=1;i<=3;i++){
//get the child dynamically from the name (
var piece:DisplayObject = getChildByName("Puz" + i + "_Level7A");
if(piece){
piece.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
}
}
If your puzzle pieces are the sole children of a common parent (let's say this code is on the timeline of that parent), you could do this:
//loop backwards through all children of this
var i:int = this.numChildren;
while(i--){
this.getChildAt(i).addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
}
If you have your pieces in an array:
var allPieces:Array = [Puz3_Level7A, Puz2_Level7A, Puz3_Level7A];
for(var i:int=0;i<allPieces.length;i++){
allPieces[i].addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
}
I upvoted #SameerJain's answer, it will do what you want. Taking it a little further, you can reduce all your repeated code with a simple function that makes your puzzle pieces draggable:
function makeDraggable(target:Sprite):void {
target.addEventListener(MouseEvent.MOUSE_DOWN, dragStart);
}
function dragStart(e:MouseEvent):void {
var target:Sprite = e.currentTarget as Sprite;
target.startDrag();
setChildIndex(target, numChildren - 1);
stage.addEventListener(MouseEvent.MOUSE_UP, dragEnd);
}
function dragEnd(e:MouseEvent):void {
stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, dragEnd);
}
makeDraggable(Puz3_Level7A);
makeDraggable(Puz2_Level7A);
makeDraggable(Puz1_Level7A);

Action Script 3.0: Stop Drag with hittestobject

how to stop drag(event) the object with hittestobject.. thanks.
object.addEventListener(TouchEvent.TOUCH_BEGIN, drag);
object.addEventListener(TouchEvent.TOUCH_END,drop);
addEventListener(Event.ENTER_FRAME, loop);
function drag(e:TouchEvent):void {
e.target.startTouchDrag(e.touchPointID);
}
function drop(e:TouchEvent):void {
e.target.stopTouchDrag(e.touchPointID);
}
function loop(e:Event):void {
if (object.hitTestObject(collision)) {
//code to stop drag event?
}
}
or is there other way to stop drag event aside from function drop?
sorry for my bad english.
//edited
In the function drop() e.target is the object that currenty processes the event. In the function loop() you also have some objects. It is not clear which of them is dragging but you should call either object.stopTouchDrag() or collision.stopTouchDrag().
UPDATE
There is an argument for both startTouchDrag and stopTouchDrag functions - touchPointID, it is used to determine what touch point (of many) is processed. When stopping the drag, you need to use the same touchPointID which was used for starting it. When calling the stopTouchDrag from a non-event context, you can't know what touch point it should use. So you need to remember it somehow. If your target object is a MovieClip you can just add a dynamic property to it and save the touchPointID there:
function drag(e:TouchEvent):void {
(e.target as MovieClip).touchPointID = e.touchPointID;
e.target.startTouchDrag(e.touchPointID);
}
function loop(e:Event):void {
if (object.hitTestObject(collision)) {
object.stopTouchDrag(object.touchPointID);
}
}

AS3 ArgumentError #2025

I'm making a game as a project for school and have recently encountered a little problem in as3. I'm making a game where you are maneuvering ship avoiding asteroids, and just added the function so that when you hit an asteroid your ship is removed from the stage via stage.removeChild. Everything is fine until you actually hit something, then this error comes up(and I mean alot, like it keeps repeating itself for as long as the game is on):
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Function/com.asgamer.basics1:Engine/private:loop/com.asgamer.basics1:krash()[C:\Users\nti\Desktop\Ship game\com\asgamer\basics1\Engine.as:54]
Here's the code(I marked out line 54):
package com.asgamer.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Engine extends MovieClip
{
private var numStars:int = 80;
private var enemyList:Array = new Array();
private var ourShip:Ship;
public function Engine() : void
{
ourShip = new Ship(stage);
stage.addChild(ourShip);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 14) == 5)
{
var enemy:Asteroid = new Asteroid(stage);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
stage.addEventListener(Event.ENTER_FRAME, krash);
function krash(e:Event):void{
if (enemy.hitTestObject(ourShip)==true){
stage.removeChild(ourShip); <-------------------------- LINE 54
}
}
}
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
}
}
Please do remember that I'm sort of a beginner to coding, which would explain other possible "faults" in the code. :)
The problem is, as was mentioned a while ago, is that you don't clean up your ourShip after actually removing it from stage. See, once you call stage.removeChild() the ship is no longer on stage, but collision check still goes because you didn't remove the enter frame listener. You should add the remove listener statement to the code branch where you remove the ship.
stage.addEventListener(Event.ENTER_FRAME, krash);
... // other code
function krash(e:Event):void{
for each (var enemy in enemyList)
if (enemy.hitTestObject(ourShip)==true){
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash); // THIS line
}
}
EDIT: first, move the addEventListener(Event.ENTER_FRAME, krash) line to the point where you add a loop listener, because you don't want to have more than a single one of these, and second, make a complete loop for hitTestObject versus enemyList. The code above is updated.
Note: Some validity checks may need to be present in the listener code, for example, if ourShip is already removed you might just return from the event listener function, or skip the hit test checks. This will also help you once you'll start advancing to a single master enter frame listener - it's generally faster having one function as event listener than many for the same event, and also helps you consolidate every bit of code you want to be executed in response for a particular event in one place, instead of searching where did you place that statement that bugs out. This also helps to not fiddle with adding/removing listeners on a regular basis, although sometimes playing with listeners can do you better.
Problem is caused by the fact that you're trying remove ourShip from the stage several times. More precisely on each Event.ENTER_FRAME event.

AS3 collision detection is not recognized

I'm quite new to AS3, and I need some help. I'm trying to make a game like Mario. I've made a character which can jump right now, but I've got some problems with collision detection.
I would like my character to jump on a bar, which is placed higher. My collision detection doesn't work at all I gues..
I've made a cirle which has a instance name mcMain and I've made a MovieClip of it. T also made a rectangle which has a instance name balkje, I also made aMovieClip of it.
I hope you can tell me what is wrong about my code and what I've to change to make the collision detection work! Thanks a lot!
balkje.addEventListener(KeyboardEvent.KEY_DOWN, drag);
stage.addEventListener(KeyboardEvent.KEY_UP, drop);
function drag(e:KeyboardEvent):void
{
e.target.startDrag();
}
function drop(e:KeyboardEvent):void
{
stopDrag();
if (balkje.hitTestObject(mcMain))
{
trace("Collision detected!");
}
else
{
trace("No collision.");
}
}
I think you should be using mouseEvent, not keyboard event.
How can you drag with the keyboard?
balkje.addEventListener(MouseEvent.MOUSE_DOWN, drag);
balkje.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
e.target.stopDrag();
if (balkje.hitTestObject(mcMain))
{
trace("Collision detected!");
}
else
{
trace("No collision.");
}
}

AS3 referencing current function

I just came across a curious scenario where I want to use removeEventListener() within a function that doesn't have a name. By this I mean, I've created the function within addEventListener(), instead of making reference to one:
addEventListener(
Event.ENTER_FRAME,
function(e:Event):void
{
if(getTimer() > 8000)
{
// removeEventListener(Event.ENTER_FRAME, <<this function>>);
// Other stuff
}
}
);
Is it possible to make a reference to the current function (ie the function I'm working within)? Or do I just need to structure the above the standard way?
Please not that I am fully aware that you can use many of the standardized methods available to achieve the above, it was purely an example snippet.
There are two options, you can either give it a name (and there are three ways to do that) or you can use arguments.callee.
In the case of the former, the three ways to name a function in AS3:
class Foo
{
// class (static or member) level
public function bar():void
{
// use a variable (technically, this function is anonymous, but we can
// still use the variable to reference the function itself.
var inVariable:Function = function():void
{
// declare it in a local scope
function local():void
{
}
}
}
}
To use a named function:
function callback(e:Event):void {
trace("tick");
removeEventListener(Event.ENTER_FRAME, callback);
}
addEventListener(Event.ENTER_FRAME, callback);
To use arguments.callee:
addEventListener(
Event.ENTER_FRAME,
function(e:Event):void
{
if(getTimer() > 8000)
{
// I get superstitious and I use a local variable.
var callee:Function = arguments.callee
removeEventListener(event.type, callee);
// Other stuff
}
}
);
You just need to give it a name, eg:
addEventListener(Event.ENTER_FRAME, function callback(e:Event):void {
trace("tick");
removeEventListener(Event.ENTER_FRAME, callback);
});
In this example "tick" will only be traced one time.
Using anonymous functions in actionscript is a bad choice, since it is really slow. Also they hardly can be garbage collected. It is also good to mention that this will only work if when the listener has been called (yes, in case of an enter_frame it will), so outside the anonymous function other functions are unable to remove the listener. Beside that, is is also a actionscript-convention to use separate functions, which makes your code more readable and it will take only a few extra chars (just to name it).
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
if(getTimer() > 8000)
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
If you want a more easy way to remove the event listener; you could detect the type and the function callee of the listener from the target object. However I think this also makes the code a bit less readable.
e.target.removeEventListener(e.type, arguments.callee);
sources
http://jacksondunstan.com/articles/413
http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html