AS3 mouse events not firing when mouseX = 0 - actionscript-3

I'm creating what should be a very simple, full screen drag and drop game in Flash Develop. It works fine except in one frustrating instance.
I add items to the stage, add MOUSE_DOWN listeners to them and start dragging when one hears that listener. I then add a MOUSE_UP listener to figure out when to stop the drag. Again, this works fine unless mouseX = 0. When the mouse is all the way to the left of the screen and I mouse up or mouse down no listener is fired. I also took it out of full screen mode and if the mouse is at or below 0 no mouse events will fire.
What in the world is going on?
private function itemSelectedHandler(e:MouseEvent):void
{
thisItem = GameItem(e.currentTarget);
thisItem.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, itemUnselectedHandler, false, 0, true);
}
private function itemUnselectedHandler(e:MouseEvent):void
{
stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, itemUnselectedHandler);
thisItem.removeEventListener(MouseEvent.MOUSE_DOWN, itemSelectedHandler);
}

You are calling stopDrag on the class and not the dragged sprite. Try something like the following :
package
{
public class Main extends Sprite
{
private var _draggedSprite:Sprite = null;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
for (var i:int = 0; i < 10; i++)
{
createBox();
}
}
private function createBox():void
{
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xff0000, 1);
sp.graphics.drawRect(0, 0, 30, 30);
sp.graphics.endFill();
sp.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
sp.x = Math.random() * (stage.stageWidth - 30);
sp.y = Math.random() * (stage.stageHeight - 30);
addChild(sp);
}
private function onMouseDown(e:MouseEvent):void
{
var sp:Sprite = e.target as Sprite;
sp.startDrag();
_draggedSprite = sp;
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseUp(e:MouseEvent):void
{
_draggedSprite.stopDrag();
_draggedSprite = null;
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
}
}
This worked for me when mouseX=0 in fullscreen mode.

Related

Away3d mesh not getting mouseClick events

I am just trying to get the coordinates of the mouse in a plane but the mouse event just doesnt fire.
I also have an starling instance on top of away3d that i got with this tutorial and i believe there is a problem there.
Here is my code:
public class Main extends Sprite
{
private var view3D:View3D;
private var stage3DManager:Stage3DManager;
private var stage3DProxy:Stage3DProxy;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
view3D = new View3D();
initProxies();
}
private function initProxies():void
{
stage3DManager = Stage3DManager.getInstance(stage);
stage3DProxy = stage3DManager.getFreeStage3DProxy();
stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
}
private function onContextCreated(event:Stage3DEvent):void
{
initAway3D();
initStarling();
var floor:Mesh = new Mesh(new PlaneGeometry(600, 400), new ColorMaterial(0x530000));
floor.mouseEnabled = true;
view3D.scene.addChild(floor);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
floor.addEventListener(MouseEvent3D.CLICK, onClick);
}
private function onClick(e:MouseEvent3D):void
{
trace("Click");
}
private function initAway3D():void
{
view3D.stage3DProxy = stage3DProxy;
view3D.shareContext = true;
addChild(view3D);
view3D.mousePicker = PickingType.SHADER;
view3D.camera = new Camera3D(new OrthographicLens());
view3D.camera.x = 1000;
view3D.camera.y = 1000;
view3D.camera.z = 1000;
view3D.camera.lookAt(new Vector3D(0, 0, 0));
}
private function initStarling():void
{
var starling:Starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.showStats = true;
starling.start();
}
private function onEnterFrame(e:Event):void
{
stage3DProxy.clear();
view3D.render();
stage3DProxy.present();
}
}
}
I'm working on a small project, I based it from this tutorial, and everything works fine!
I had a problem with feathers but I resolved it when I called starling.start() which is not in the tutorial, but you've already did it :)
Try this :
private function initStarling():void
{
starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.addEventListener(Event.ROOT_CREATED, rootCreatedHandler);
}
private function rootCreatedHandler(event:Event):void
{
starling.removeEventListener(Event.ROOT_CREATED, rootCreatedHandler);
stage3DProxy.addEventListener(flash.events.Event.ENTER_FRAME, onEnterFrame);
starling.start();
}
private function onEnterFrame(event:flash.events.Event):void
{
view3D.render();
starling.nextFrame();
}
I hope that it will help you!

As3 hittesting doesn't really work (nested)

I got a tank.
I got a hero (controllable character).
The tank has a nested movieclip which has a very thin surface area.
Yet, the thing detects a collision when it's not even touching the hero.
public class tank_sight extends MovieClip
{
private var _root:MovieClip;
public function tank_sight()
{
addEventListener(Event.ADDED, beginClass);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event:Event):void
{
if(this.hitTestObject(_root.hero.hitbox))
{
this.gotoAndStop(2);
trace("HIT");
fire();
}
else
{
this.gotoAndStop(1);
}
}
private function fire():void
{
var shell:Shell = new Shell(x, y, rotation - 180);
_root.addChild(shell);
}
}
What's wrong? I don't get it.
EDIT: Sight is rotating, so that's probably why. I tried using this code on the player class:
point = _root.tanks.barrel.sight.localToGlobal(new Point());
if(this.hitTestPoint(point.x, point.y, false))
{
trace("HIT");
}
But it doesn't work.. It never traces "HIT", unless I stand in some weird location at certain times.
hitTestObject works with nested objects also (display objects that have different parents), you should check logic of your game, and do some tests.
Simple example:
var squareHolder:Sprite = new Sprite();
var squareInner:Shape = new Shape();
var hitHolder:Sprite = new Sprite();
var hitCircle:Shape = new Shape();
hitCircle.graphics.beginFill(0x990000);
hitCircle.graphics.drawCircle(0, 0, 20);
squareInner.graphics.beginFill(0x009900);
squareInner.graphics.drawRect(0, 0, 40, 40);
addChild(squareHolder);
squareHolder.addChild(squareInner);
squareHolder.x = 200;
squareHolder.y = 100;
squareInner.x = 50;
squareInner.y = 50;
stage.addChild(hitHolder);
hitHolder.addChild(hitCircle);
hitCircle.transform.matrix = new Matrix(1, 0.5, 0.5, 1, 30, 30);
stage.addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void {
hitHolder.x = e.stageX;
hitHolder.y = e.stageY;
if (hitCircle.hitTestObject(squareInner)) {
trace("Ding!");
}
});
Whatever you do with hitCircle (visible=false, trasparent fill, transformations) it will still work.

as3 Error 1009 at Coin1/coin1go(), i am trying to get an enemy to drop a coin

So the enemy does drop a coin but i does not get the properties of the coin( if it hits the player it gives him +5 coins)
The coin will be removed if it hits the bottom of the stage, if the player dies or if the player hits it. Sadly, it does not work.
But this does work if i place a coin on the stage before i start the game, it gets all its properties, so then it must be the moment it gets added to the stage that it does not get linked with the coding or something..... and that is where i am right now.
this is the .as file for the coin:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Coin1 extends MovieClip
{
private var _root:Object;
private var speed:int = 0;
public function Coin1()
{
addEventListener(Event.ENTER_FRAME, Speed1);
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, coin1go);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
}
private function Speed1(event:Event):void
{
y += speed;
}
private function coin1go(event:Event):void
{
if (this.y > stage.stageHeight)
{
removeEventListener(Event.ENTER_FRAME, coin1go);
_root.removeChild(this);
}
if (hitTestObject(_root.player_mc))
{
_root.coin += 1;
removeEventListener(Event.ENTER_FRAME, coin1go);
_root.removeChild(this);
}
if (_root.playerhealth <= 1)
{
removeEventListener(Event.ENTER_FRAME, coin1go);
_root.removeChild(this);
}
}
}
}
This is the part from where it gets added to the stage:
if (enemy2health <= 0)
{
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.score += _root.Enemy2Score * _root.scoremultiplyer;
stage.addChild(newExplosionSmall)
newExplosionSmall.x = this.x;
newExplosionSmall.y = this.y;
stage.addChild(newCoin1)
newCoin1.x = this.x;
newCoin1.y = this.y;
Ass you can see there is also an addchild for an explosion wich works perfectly fine but that may jus be because it does nothing else than appear and remove itself.
So long story short: enemy drops coin but it does nothing and floats to the bottom of the screen and i get a constant stream of 1009 errors. so does anyone know how to fix this?
You should add an enterframe listener only after receiving a valid stage reference, which only appears when Event.ADDED_TO_STAGE event is received. This is because your enterframe listener refers stage.
public function Coin1()
{
addEventListener(Event.ENTER_FRAME, Speed1);
addEventListener(Event.ADDED_TO_STAGE, beginClass);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
addEventListener(Event.ENTER_FRAME, coin1go);
}

MOUSE_DOWN trigger every frame, ActionScript 3

as I am new to as3, I want to implement a MachineGun, that fires while has ammo, and the trigger is pulled, In my case, the MouseEvent.MOUSE_DOWN.
The problem is that this event fires only once.
The closest I get is to this is MouseEvent.MOUSE_MOVE, but it fails my purpose when the mouse position is sustained
EDIT:
I need the updated mouse cursor every frame
When your MouseEvent.MOUSE_DOWN event handler you can create an event listener that listens for the Event.ENTER_FRAME event to be dispatched. Using the Event.ENTER_FRAME event handler you can repeatedly call a method that handles shooting the bullet. The following is an example I modeled after an example at http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width="500", height="500", frameRate="24", backgroundColor="0xFFFFFF")]
public class Main extends Sprite
{
private var _gun:Gun;
private var _interval:int;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_gun = new Gun();
_gun.x = stage.stageWidth / 2 - _gun.width / 2;
_gun.y = stage.stageHeight / 2 - _gun.height / 2;
addChild(_gun);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
}// end function
private function onStageMouseDown(e:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
}// end function
private function onStageEnterFrame(e:Event):void
{
shootBullet();
}// end function
private function shootBullet():void
{
if (_interval > 5)
{
var bullet:Bullet = new Bullet();
bullet.addEventListener(Event.ENTER_FRAME, onBulletEnterFrame);
bullet.x = _gun.x;
bullet.y = _gun.y;
bullet.angleRadian = Math.atan2(stage.mouseY - _gun.y, stage.mouseX - _gun.x);
bullet.addEventListener(Event.ENTER_FRAME, onBulletEnterFrame);
addChild(bullet);
_interval = 0; // reset
}// end if
_interval++;
}// end function
private function onBulletEnterFrame(e:Event):void
{
var bullet:Bullet = Bullet(e.target);
bullet.x += Math.cos(bullet.angleRadian) * bullet.SPEED;
bullet.y += Math.sin(bullet.angleRadian) * bullet.SPEED;
if ( bullet.x < 0 || bullet.x > 500 || bullet.y < 0 || bullet.y > 500)
{
removeChild(bullet);
bullet.removeEventListener(Event.ENTER_FRAME, onBulletEnterFrame);
}// end if
}// end function
private function onStageMouseUp(e:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, onStageEnterFrame);
stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
_interval = 0;
}// end function
}// end class
}// end package
import flash.display.Sprite;
internal class Gun extends Sprite
{
public function Gun()
{
graphics.beginFill(0x000000);
graphics.drawCircle(0, 0, 10);
graphics.endFill();
}// end function
}// end class
internal class Bullet extends Sprite
{
public var angleRadian:Number;
public const SPEED:Number = 10;
public function Bullet()
{
graphics.lineStyle(1, 0x000000);
graphics.drawCircle(0, 0, 10);
graphics.endFill();
}// end function
}// end class
The point of interest is in the onStageEnterFrame() method. There's an if statement that checks whether the _interval property's value is greater than 5, if so, a new bullet is created and shot otherwise the _interval property's value is incremented. The purpose of the _interval property is to space out the bullets being shot.
[UPDATE]
Here is an image of the example flash application running:
The mouse button is being held down in the top right corner hence the bullets being shot in that direction.
On receiving a MouseEvent.MOUSE_DOWN event, run a while loop making the machine gun fire. The loop breaks on receiving a MouseEvent.MOUSE_UP event. Something like this
private function handleMouseDown( event:Event ):void
{
this.addEventListener( MouseEvent.MOUSE_UP , handleMouseUp );
startFiring();
}
private function handleMouseUp( event:Event ):void
{
this.removeEventListener( MouseEvent.MOUSE_UP , handleMouseUp );
this.addEventListener( MouseEvent.MOUSE_DOWN , handleMouseDown);
stopFiring();
}
EDIT: For clarification, the stop and start firing functions are functions that run a loop to keep the machine gun firing animation going however that may be.
add firing action to ENTER_FRAME event when mouse is pressed and remove when mouse is up
this.addEventListener( MouseEvent.MOUSE_DOWN, this.handler_down );
this.addEventListener( MouseEvent.MOUSE_UP, this.handler_up );
private function handler_down(event:Event):void {
super.addEventListener( Event.ENTER_FRAME, this.handler_frame );
}
private function handler_up(event:Event):void {
super.removeEventListener( Event.ENTER_FRAME, this.handler_frame );
}
private function handler_frame(event:Event):void {
this.fire();
}
I have to do this regularly, and instead of using an enter_frame listener, I opt for a Timer() and TimerEvent.Timer listener. That way I have more control over how often the repeat is fired without accounting for frame rate and what not.
private var fireRepeat:Timer;
private function triggerDown(e:MouseEvent):void {
if (!this.fireRepeat) { // create the repeater
this.fireRepeat = new Timer(500, 0);
this.fireRepeat.addEventListener(TimerEvent.TIMER, this.fire);
}
fire(); // fire the first bullet
this.fireRepeat.reset(); // reset the repeater
this.fireRepeat.start(); // start the repeater
}
private function triggerUp(e:MouseEvent):void {
if (this.fireRepeat) { this.fireRepeat.stop(); }
}
public function fire(e:* = null):void {
trace('fire!');
}

ActionScript - Mouse Logic Problem

dumb problem. embarrassed that i haven't found a solution. i'm tired.
a red square in on the stage. mouse-down + mouse-drag-up will move the red square downward (+y), while mouse-down + mouse-drag-down will move the red square upward (-y). this opposite motion is desired.
however, during a mouse drag the square must begin moving from it's current y position, regardless of how many mouse drags have changed it's initialized position. currently, the red square will always begin at stage 0, since my mouseDownOrigin variable is incorrect because my brain is asleep.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Test extends Sprite
{
private var sp:Sprite = new Sprite();
private var mouseDownOrigin:int;
public function Test()
{
sp.graphics.beginFill(0xFF0000);
sp.graphics.drawRect(0, 0, 100, 100);
sp.x = sp.y = 200;
addChild(sp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
}
private function mouseDownEventHandler(evt:MouseEvent):void
{
mouseDownOrigin = evt.stageY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
}
private function mouseMoveEventHandler(evt:MouseEvent):void
{
sp.y = mouseDownOrigin - evt.stageY;
}
private function mouseUpEventHandler(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
}
}
}
You need to record the red square y position on the MouseUp event handler, practically recording the position you leave the square in.
private var currentPosition:int;
private function mouseUpEventHandler(evt:MouseEvent):void
{
currentPosition = sp.y;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
}
private function mouseMoveEventHandler(evt:MouseEvent):void
{
var n:int = event.stageY - mouseDownOrigin ;
sp.y = currentPosition - n;
}