MouseDown event not firing - actionscript-3

I have hundreds of movieclips constantly being created on a timer, each with a mouseDown event listener which will remove the movieclip on mouseDown. For nearly all the movieclips the mouseDown event seems to fire correctly, however sometimes I notice that the mouseDown event does not fire for a movieclip (i.e. it is not removed).
This is function creating the movieclip:
public function pickShape():MovieClip {
//possible shapes
var shapes:Array = [new triangle(), new rectangle(), new square()];
var randomCol:int = Math.floor(Math.random()*colours.length);
var randomShape:int = Math.floor(Math.random()*shapes.length);
var chosenShape:MovieClip = shapes[randomShape];
//change shape to random colour
var shapeCol:ColorTransform = chosenShape.transform.colorTransform;
shapeCol.color = colours[randomCol];
chosenShape.transform.colorTransform = shapeCol;
chosenShape.addEventListener(MouseEvent.MOUSE_DOWN, destroyShape); //remove mc
return chosenShape;
}
pickShape() is called from another function which is called on a timerEvent, in this other function the scaleX, scaleY and rotation of chosenShape are altered. chosenShape will be moving across the screen in an EnterFrame, when it is offscreen it is removed.
What would cause this?
thanks for any help

Does your destroyShape function looks like this:
function destroyShape(e:MouseEvent):void
{
e.currentTarget.parent.removeChild(e.currentTarget);
}
?

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);

Actionscript 3 - How do I make new instances draggable?

I am attempting to build a drag and drop game where a user can build something using the images I provide. I will have images in a menu that the user can click and drag to the building area. The user will be able to add however many instances of that image as they want.
I was able to get part of it working. So far, I can click the image and drag it around, and create as many instances as I want. However, I cannot click and drag the image once I have placed it.
When I do a trace to see what the name is, it says that all the new instances are named hillChild1. I tried to make them named hillChild1, hillChild2, etc., but that doesn't seem to work either... not sure that is an issue, though.
Here's my code:
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
var myImage:Sprite = Sprite(new Hill_mc());
var i:Number=0; i++;
function onDown(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.name = "hillChild"+i;
addChild(myImage);
myImage.x = mouseX;
myImage.y = mouseY;
myImage.startDrag();
myImage.buttonMode = true;
}
function onUp(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.stopDrag();
myImage.name = "hillChild";
}
stage.addEventListener(MouseEvent.CLICK, traceName);
function traceName(event:MouseEvent):void { trace(event.target.name); }
myImage.getChild(myImage).addEventListener("mouseDown", mouseDownHandler);
stage.addEventListener("mouseUp", mouseUpHandler);
function mouseDownHandler (e:MouseEvent):void{
myImage.startDrag();
}
function mouseUpHandler (e:MouseEvent):void{
myImage.stopDrag();
}
I am new to StackOverflow and also Actionscript 3, if it isn't apparent.
Your issue is likely that you are creating a new instance on mouse up (when what you want is a reference to instance that was already created on mouse down). Also, you never add a click listener to you new objects. Add the mouse up listener to stage only after the mouse down (then remove the listener in the mouse up).
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, createCopy);
var i:int=0;
var tmpImage:Sprite; //to store which image is being dragged currently
function createCopy(e:MouseEvent):void {
tmpImage = new Hill_mc();
tmpImage.name = "hillChild"+(i++); //increment every copy
addChild(tmpImage);
tmpImage.x = mouseX;
tmpImage.y = mouseY;
tmpImage.startDrag();
tmpImage.buttonMode = true;
tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); //add the mouse down to this new object
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //since the mouse is currently down, we need to listen for mouse up to tell the current copy to stop dragging
}
//this will be called when click a copy
function onDown(e:MouseEvent):void {
tmpImage = Sprite(e.currentTarget); //get a reference to the one that was clicked, so we know which object to stop dragging on the global mouse up.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //listen for the mouse up
tmpImage.startDrag();
}
function onUp(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP,onUp); //now that the mouse is released, stop listening for mouse up
tmpImage.stopDrag(); //stop dragging the one that was clicked
}

How to select multiple objects on MOUSE_OVER during a MOUSE_DOWN

Lets say I have 30 objects created in for loop, added to a container.
Objects stop on frame 1. I have added event listeners to the objects as you can see below, and when I click any object inside container, it goes to frame 2 and play.
for (var i:int=0; i < 30; i++)
{
var object = new Object1();
object.gotoAndStop(1);
object.addEventListener(MouseEvent.CLICK, myFunction);
container.addChild(object);
}
private function myFunction(e:MouseEvent):void
{
e.currentTarget.gotoAndPlay(2);
}
So I have to click each object to send it on frame 2,
I also tried ROLL_OVER, everything is same but CLICK is changed to ROLL_OVER inside for loop.
What I want is to click, and then mouse over object so they go to frame 2 and play.
The problem is that I need to use MOUSE_DOWN event, I have tried to set MOUSE_DOWN instead of CLICK or ROLL_OVER, but it does not work. If I want to send objects to frame 2 (using MOUSE_DOWN), I need to click each of them, there is no difference between MOUSE_DOWN and CLICK in this case.
As someone who does not know much about mouse events, I'm wondering why roll over and click works, but mouse_down does not?
I think I see what you're trying to do... you want to press the mouse to start drawing over a bunch of sprites, each one goes to frame two when you mouse over it, but only if the mouse button is pressed, right?
try something like this
container.addEventListener(MouseEvent.MOUSE_DOWN, setMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, setMouseUp);
private var mouseIsDown:Boolean = false;
private var currentSprite:Sprite;
for (var i:int=0; i < 30; i++)
{
var object = new Object1();
object.gotoAndStop(1);
object.addEventListener(MouseEvent.MOUSE_OVER, myFunction);
object.mouseChildren = false;
container.addChild(object);
}
private function setMouseDown(e:MouseEvent){
mouseIsDown = true;
setActive(currentSprite);
}
private function setMouseUp(e:MouseEvent){
mouseIsDown = false;
}
private function myFunction(e:MouseEvent){
currentSprite = e.target;
if(mouseIsDown){
setActive(currentSprite);
}
}
private function setActive(target:Sprite){
target.gotoAndPlay(2);
}

AS3 - MouseEvent.CLICK fires off for mouse click that happened before the listener was added

I'm new to AS3 and have made a simple "asteroids" game with a game over screen and a resetButton that lets the user play again. When the user clicks on the reset button, the game over screen and the reset button are removed from the stage, and the game proper is added to the stage, along with eventListeners. One of these is a MouseEvent.CLICK listener added to the stage, which calls a fireBullet function. This function draws a bullet and adds it to the stage (other parts of the code then make the bullet move on the screen).
The issue that I am having is that when the user clicks on the reset button, the gameover screen is removed correctly, and the game proper (player, asteroids, eventListeners) are added to the stage correctly, but also at the same time a bullet fires even though the user has not clicked after clicking on the reset button.
My gameOver() function is like this:
stage.removeChild() all objects
stage.removeEventListener() all listeners
null out all objects
draw and add to the stage the game over text and resetButton
addEventListener(MouseEvent.CLICK, onReset) to the resetButton
Then, the onReset() function looks like this:
stage.removeChild() the gameover text and the resetButton
call gameStart();
The gameStart() function looks like this:
initialize variables
draw and add player and asteroids on the screen
add eventListeners including MouseEvent.Click, fireBullet
I've added traces at each function to see what's going on, and this is the flow:
added fireBullet listener //this is gameStart() function being called from Main() and adding everything to the stage the first time
fired bullet //shooting at the asteroids
fired bullet
fired bullet
fireBullet listener should have been removed //this is gameOver() being called that removes everything from the stage and adds the resetButton
clicked on reset
added fireBullet listener //gameStart() being called again from onReset() function
fired bullet //I did not click a second time after clicking on reset
I've read somewhere that events are dispatched all the time regardless if any listeners are actually listening for them, so my suspicion is that my MouseEvent.CLICK listener is picking up the mouse button click from the time when the reset button is clicked, even though this listener is added to the stage afterwards.
I don't have enough experience with AS3 or programming to figure out if this is really the case and what can I do to make sure that the MouseEvent.CLICK listener does not respond to any clicks that happened before it was added to the stage, so any help with this would be greatly appreciated.
====
EDIT
I was assuming I had a logic problem or didn't know something about AS3 and flash, so I just used pseudo code above. Below is a link to the full .as file including the generated .swf
And below that are the relevant functions in full
https://www.dropbox.com/sh/a4rlasq8o0taw82/wP3rB6KPKS
private function startGame():void this is called from Main
{
//initialize variables
bulletArray = [];
cleanupBullets = [];
bulletSpeed = 10;
score = 0;
asteroid1Speed = 0;
asteroid2Speed = 0;
asteroid3Speed = 0;
asteroid4Speed = 0;
//draw player and asteroids
ship = drawPlayer();
asteroid1 = drawAsteroid();
asteroid2 = drawAsteroid();
asteroid3 = drawAsteroid();
asteroid4 = drawAsteroid();
//embarrasing and inefficient code to get random number between -5 and 5 without a 0
asteroid1Speed = Math.ceil(Math.random() * 10 -5);
if (asteroid1Speed == 0)
asteroid1Speed = returnNonZero(asteroid1Speed);
asteroid2Speed = Math.ceil(Math.random() * 10 -5);
if (asteroid2Speed == 0)
asteroid2Speed = returnNonZero(asteroid2Speed);
asteroid3Speed = Math.ceil(Math.random() * 10 -5);
if (asteroid3Speed == 0)
asteroid3Speed = returnNonZero(asteroid3Speed);
asteroid4Speed = Math.ceil(Math.random() * 10 -5);
if (asteroid4Speed == 0)
asteroid4Speed = returnNonZero(asteroid4Speed);
//trace(asteroid1Speed, asteroid2Speed, asteroid3Speed, asteroid4Speed);
//add asteroids to stage
stage.addChild(asteroid1);
stage.addChild(asteroid2);
stage.addChild(asteroid3);
stage.addChild(asteroid4);
//position player and add to stage
ship.x = 40;
ship.y = 40;
stage.addChild(ship);
//add event listeners
stage.addEventListener(Event.ENTER_FRAME, onFrame);
stage.addEventListener(MouseEvent.CLICK, fireBullet);
trace("added fireBullet listener");
}
private function gameOver():void this is called from an onFrame(called every frame) function that I am not including (it's too big and not exactly relevant). it's called when all asteroids are removed.
{
//remove any remaining bullets off the screen
for each (var item:Sprite in bulletArray)
{
stage.removeChild(item);
}
//null out objects and remove listeners
bulletArray = null;
stage.removeEventListener(Event.ENTER_FRAME, onFrame);
stage.removeEventListener(MouseEvent.CLICK, fireBullet);
//check if the listener has actually been removed
if (!(stage.hasEventListener(MouseEvent.CLICK))) {
trace("fireBullet listener should have been removed");
}
stage.removeChild(ship);
ship = null
//graphic for resetButton
resetButton = new Sprite();
resetButton.graphics.beginFill(0xFFFFFF);
resetButton.graphics.drawRect(0, 0, 100, 50);
resetButton.graphics.endFill();
//position for resetButton
resetButton.x = 250;
resetButton.y = 300;
//text for resetButton
resetTextField = new TextField();
var resetTextFormat:TextFormat = new TextFormat();
resetTextFormat.size = 30;
resetTextFormat.color = 0x000000;
resetTextField.defaultTextFormat = resetTextFormat;
resetTextField.selectable = false;
resetTextField.text = "RESET";
resetButton.addChild(resetTextField);
//add resetButton and listener
stage.addChild(resetButton);
resetButton.addEventListener(MouseEvent.CLICK, onReset);
//gameover text
gameOverTxtField = new TextField();
gameOverFormat = new TextFormat();
gameOverFormat.size = 50;
gameOverFormat.color = 0xFFFFFF;
gameOverFormat.align = "center";
gameOverTxtField.defaultTextFormat = gameOverFormat;
gameOverTxtField.selectable = false;
gameOverTxtField.text = "GAME OVER";
gameOverTxtField.width = 660;
gameOverTxtField.height = 200;
gameOverTxtField.x = -10;
gameOverTxtField.y = 20;
stage.addChild(gameOverTxtField);
}
private function onReset(e:MouseEvent):void
{
trace("clicked on reset");
//remove gameover objects and null them
resetButton.removeEventListener(MouseEvent.CLICK, onReset);
stage.removeChild(gameOverTxtField);
stage.removeChild(resetButton);
resetButton = null;
gameOverTxtField = null;
//restart the game
startGame();
}
What's happening is that MouseEvent.CLICK is a bubbling event. In Flash, events have three phases: the "capture phase", the "at target" phase, and "bubbling phase". You can read about it in this Adobe article.
Your reset button's click event handler happens in the "at target" phase. If you trace out the event's phase in the reset button click handler, it will show that event.phase is 2. Per the docs, 1 = "capture phase", 2 = "at target", 3 = "bubbling phase".
After the reset button click handler does its work, the event then bubbles back up through the display list. Since the stage is at the top of the display list, the click event "bubbles up" to the stage. And by that time, you've started the game again and added the stage's click event handler. So the stage's click handler is also triggered.
You can confirm this by tracing out the value of event.phase in your bulletFired() method:
private function fireBullet(e:MouseEvent):void
{
// most of this time it will trace out 2 for the phase
// except when you click on an asteroid when firing or
// click the reset button
trace("fired bullet, event phase: " + e.eventPhase);
bullet = drawBullet();
bullet.y = ship.y;
bullet.x = ship.x + (ship.width / 2);
bulletArray.push(bullet);
stage.addChild(bullet);
}
To fix the problem, you can stop the event from bubbling in your onReset() method:
private function onReset(e:MouseEvent):void
{
// prevent this event from bubbling
e.stopPropagation();
trace("clicked on reset");
//remove gameover objects and null them
resetButton.removeEventListener(MouseEvent.CLICK, onReset);
stage.removeChild(gameOverTxtField);
stage.removeChild(resetButton);
resetButton = null;
gameOverTxtField = null;
//restart the game
startGame();
}
It sounds to me like the previous iteration of your game has not had the MOUSE.CLICK event listener removed. Even if the game is removed, the MOUSE.CLICK event will continue triggering whatever handler you added to it, eg; addEventListener(MOUSE.ClICK, ). When the game is removed you also need to removeEventListener(MOUSE.CLICK, ).

mouse up event not working properly

can anyone please help me on this.
attached is the fla which has a part of code i am working on for a project.
with help of mouse you can draw a circle on the image, but for some reasons the mouse up event does not work. it works fine when the eventlisteners is attached to the stage, but does not work when its attached to the movieclip.
also how can i restrict the circle to be drawn only inside the movieclip which is a rectangle.
here is the code
const CANVAS:Sprite = new Sprite();
var _dragging:Boolean = false;
var _corner:Point;
var _corner2:Point;
menFront.addEventListener(MouseEvent.MOUSE_DOWN, setAnchor);
menFront.addEventListener(MouseEvent.MOUSE_UP, completeRect);
function setAnchor(e:MouseEvent):void{
trace("mouse down");
if(!_dragging){
CANVAS.graphics.clear();
_corner = new Point(e.stageX, e.stageY);
_dragging = true;
menFront.addEventListener(MouseEvent.MOUSE_MOVE, liveDrag);
}
}
function completeRect(e:MouseEvent):void{
trace("mouse up");
if(_dragging){
_dragging = false;
menFront.removeEventListener(MouseEvent.MOUSE_MOVE, liveDrag);
CANVAS.graphics.lineStyle(0, 0, 0);
CANVAS.graphics.beginFill(0x222222,.5)
_corner2 = new Point(e.stageX, e.stageY);
trace(Point.distance(_corner,_corner2).toFixed(2));
CANVAS.graphics.drawCircle(_corner.x, _corner.y, Point.distance(_corner,_corner2));
addChild(CANVAS);
}
}
function liveDrag(e:MouseEvent):void{
CANVAS.graphics.clear();
CANVAS.graphics.lineStyle(0, 0x999999);
_corner2 = new Point(e.stageX, e.stageY);
//trace(Point.distance(_corner,_corner2).toFixed(2));
CANVAS.graphics.drawCircle(_corner.x, _corner.y, Point.distance(_corner,_corner2));
addChild(CANVAS);
}
If you add the MouseEvent.MOUSE_UP to the object that you are dragging, the event will only fire if the item is underneath the mouse at the moment the MOUSE_UP happens, but since you're updating the item with MOUSE_MOVE, that's a race-condition between when the MOUSE_UP happens and when the MOUSE_MOVE happens.
To avoid such problems you want to guarantee that you receive the MOUSE_UP whenever MOUSE_UP fires, during your live-update cycle. To do that, add the event listener to the stage just when it's needed, something like this:
menFront.addEventListener(MouseEvent.MOUSE_DOWN, setAnchor);
function setAnchor(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, completeRect);
// your other functionality
}
function completeRect(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, completeRect);
// your other functionality
}
so that your completeRect doesn't get called inadvertently if you're clicking elsewhere.
Hope This Helps