AS3 MOUSE_DOWN Sprite not working - actionscript-3

For the life of me I can't remember how to make a sprite respond to MOUSE_DOWN and MOUSE_UP...works if I link the listener to the stage but not a sprite...What am I doing wrong?
private function allClassesReady(e:Event):void
{
userName00 = e.currentTarget.userName0;
chatArea00 = e.currentTarget.ui0.chatArea0;
chatInput00 = e.currentTarget.ui0.chatInput0;
btnArr00 = e.currentTarget.ui0.btnArr0;
userList00 = e.currentTarget.ui0.userList0;
msgSo00 = e.currentTarget.shaob0.msgSo0;
userListSo00 = e.currentTarget.shaob0.userListSo0;
main00.stage.addEventListener(KeyboardEvent.KEY_DOWN, displayKey);
main00.stage.addEventListener(MouseEvent.MOUSE_WHEEL, scrollsTxtArea, false, 0, true);
/*listens for the enter key on the keyboard so we can send message when pressed*/
btnArr00[0].addEventListener(MouseEvent.MOUSE_DOWN, sendMsg, false, 0, true);
btnArr00[0].addEventListener(MouseEvent.MOUSE_UP, sendBtnRelease, false, 0, true);
}

Related

Is it possible to destroy a dispatched event

I'm making a game in AS3.
I've got a class named "UseBox" that dispatch an event.
If thisThing.buyable not event is dispatch and the child "Building" is visible whith 2 buildings visible and 2 not visible.
If thisThing.destructible then the event "upgradable" is dispatch and the child "Building" is visible whith 4 buildings visible.
The problem is that the dispatch "upgradable" is always on now.
Is it possible to destroy the event each time the child "Building" become visible ?
Thanx for your answers,
Here's my code :
UseBox
public function UseBox(stageRef:Stage, thisThing:Object){
if (thisThing.buyable){
useButton.gotoAndStop("buy");
useButton.addEventListener(MouseEvent.CLICK, buyIt, false, 0, true);
}
if (thisThing.destructible){
this.gotoAndStop("upgrade");
upgradeButton.buttonMode = true;
upgradeButton.addEventListener(MouseEvent.CLICK, upgradeIt, false, 0, true);
}
public function buyIt(e:MouseEvent):void{
boxClicked();
showBatiments();
lookButton.removeEventListener(MouseEvent.CLICK, buyIt);
}
public function upgradeIt(e:MouseEvent):void{
boxClicked();
showBatiments();
stageRef.dispatchEvent(new Event("upgradable"));
lookButton.removeEventListener(MouseEvent.CLICK, buyIt);
}
private function showBatiments():void{
Engine.batiments.visible = true;
}
Oh, and my buidling.as code :
poulaillerPlusBtn.visible = false;
poulaillerImpossible.visible = true;
poulaillerBtn.addEventListener(MouseEvent.CLICK, poulaillerConstruction, false, 0, true);
stageRef.addEventListener("upgradable", checkConstruction, false, 0, true);
private function checkConstruction(Event):void{
trace("I've heard upgradable");
poulaillerPlusBtn.visible = true;
poulaillerImpossible.visible = false;
poulaillerPlusBtn.addEventListener(MouseEvent.CLICK, poulaillerAmelioration, false, 0, true);
trace("on a vérifé, tu peux améliorer le batiment");
}
So, if the event "upgradable" is heard, the function "checkConstruction" is called, else not.
But, once the event "upgradable" has been dispatched, it seems that it stay always dispatch so the function "checkConstruction" is always called when "Building" is visible...
I'm not 100% sure this is what you're looking for, but if your only looking to handle an event once, then once you've recieved it by checkConstruction being called, you can remove it from listening for the event again:
private function checkConstruction(Event):void{
//Here, you would remove the function from listening to any more "upgradable" events being dispatched.
stageRef.removeEventListener("upgradable", checkConstruction);
trace("I've heard upgradable");
poulaillerPlusBtn.visible = true;
poulaillerImpossible.visible = false;
poulaillerPlusBtn.addEventListener(MouseEvent.CLICK, poulaillerAmelioration, false, 0, true);
trace("on a vérifé, tu peux améliorer le batiment");
}

remove or destroy child completely

I've got a child named "viseur" that appears on a particular scene and replace my actual cursor.
When the event "removeViseur" is called, I would like to completely remove the child "viseur" (and I'll use my previous cursor).
I did that :
stage.addEventListener("functionCalled", removeViseur, false, 0, true);
public function removeViseur(e:Event):void{
trace("removeViseur");
viseur.visible = false;
viseur.parent.removeChild(viseur);
viseur = null;
}
The child "viseur" is no longer here, but if do Alt+tab or changing window and came back, the child "viseur" come back...
Do you know how I can destroy it completely ? (I don't need it after this function is called)
Thx,
EDIT
Here is more of my code.
public static var viseur:Viseur;
var lookCocoDessous:Boolean = thisBack == "cocotierDessous";
if (lookCocoDessous) {
viseur = new Viseur(stage);
stage.addChild(viseur);
viseur.visible = true;
stage.addEventListener("functionCalled", removeViseur, false, 0, true);
public function removeViseur(e:Event):void{
trace("removeViseur");
viseur.visible = false;
viseur.parent.removeChild(viseur);
viseur = null;
}
And "viseur" has is own class like that
viseur.as :
public class Viseur extends MovieClip
{
private var engine:Engine;
private var stageRef:Stage;
private var p:Point = new Point();
public function Viseur(stageRef:Stage)
{
Mouse.hide(); //make the mouse disappear
mouseEnabled = false; //don't let our cursor block anything
mouseChildren = false;
this.stageRef = stageRef;
x = stageRef.mouseX;
y = stageRef.mouseY;
stageRef.addEventListener(MouseEvent.MOUSE_MOVE, updateMouse, false, 0, true);
stageRef.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler, false, 0, true);
stageRef.addEventListener(Event.ADDED, updateStack, false, 0, true);
}
private function updateStack(e:Event) : void
{
stageRef.addChild(this);
}
private function mouseLeaveHandler(e:Event) : void
{
visible = false;
Mouse.show(); //in case of right click
stageRef.addEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler, false, 0, true);
}
private function mouseReturnHandler(e:Event) : void
{
visible = true;
Mouse.hide(); //in case of right click
removeEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler);
}
private function updateMouse(e:MouseEvent) : void
{
x = stageRef.mouseX;
y = stageRef.mouseY;
e.updateAfterEvent();
}
}
}
}
Viseur is adding itself to the stage whenever Event.ADDED fires. This fires whenever anything is added to the stage (not just Viseur). You've also made Viseur a static property, thus ensuring it doesn't disappear from memory.
To be succinct: add Viseur only once, and from outside of itself (thereby removing the need to pass a stage reference). Be aware that once a DisplayObject is parented, it automatically gains stage & root properties which you can reference for mouseX and mouseY.
That said, if you're trying to replace the mouse cursor, I highly recommend taking a different tact: Native cursors.
See http://inflagrantedelicto.memoryspiral.com/2012/07/as3-quickie-native-mouse-cursors/

Error #1063 with removeEventListener

I'm making a game in AS3.
I've got in my main class this function :
public function tire(e:MouseEvent):void{
puzzle.removeListeners();
}
And in my Puzzle class that :
public function removeListeners():void{
var cocoUn;
var cocoDeux;
var cocoTrois;
var cocoQuatre;
var cocoCinq;
for (var i in Engine.usableItems){ // Ditto
if (Engine.usableItems[i].displayName == "COCOUN")
cocoUn = Engine.usableItems[i];
if (Engine.usableItems[i].displayName == "COCODEUX")
cocoDeux = Engine.usableItems[i];
if (Engine.usableItems[i].displayName == "COCOTROIS")
cocoTrois = Engine.usableItems[i];
if (Engine.usableItems[i].displayName == "COCOQUATRE")
cocoQuatre = Engine.usableItems[i];
if (Engine.usableItems[i].displayName == "COCOCINQ")
cocoCinq = Engine.usableItems[i];
}
cocoUn.removeEventListener(MouseEvent.CLICK, shoot, false, 0, true);
cocoDeux.removeEventListener(MouseEvent.CLICK, shootDeux, false, 0, true);
cocoTrois.removeEventListener(MouseEvent.CLICK, shootTrois, false, 0, true);
cocoQuatre.removeEventListener(MouseEvent.CLICK, shootQuatre, false, 0, true);
cocoCinq.removeEventListener(MouseEvent.CLICK, shootCinq, false, 0, true);
}
I want my 5 items to be unclickable when the function "tire" is called in my main class.
I've got an error in debug mode.
When I click on the stage, this error appear : Error #1063: Argument count mismatch on flash.events::EventDispatcher/removeEventListener(). Expected 2, got 5.
Do you know how I can correct that ?
Thank you very much,
Method removeEventListener has only three arguments:
function removeEventListener(type:String,listener:Function,useCapture:Boolean = false):void
It should be enough to ajdust your code in the following way:
cocoUn.removeEventListener(MouseEvent.CLICK, shoot);

Getting a size from a loaded file via a File variable type ActionScript3

Cannot get my file size! I have a variable that loads the file, and then on my fileCompleteLoad event i want to check the size of that file (.png).
// clickButton event to load the file
public function onMouseClick(e:MouseEvent):void{
_fileRef = new File();
_fileRef.addEventListener(Event.SELECT, onFileSelected, false, 0, true);
_fileRef.addEventListener(Event.CANCEL, onCancel, false, 0, true);
_fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
_fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
onSecurityError, false, 0, true);
_fileRef.browse([_imageFilter]);
}
// selected event
public function onFileSelected(evt:Event):void
{
_fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress, false, 0, true);
_fileRef.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
_fileRef.load();
}
// thats my eventComplete
public function onComplete(evt:Event):void
{
_msgSuccessErrorTextField.text = "File was successfully loaded.";
_pngInputTextField.text = String(_fileRef.nativePath);
_atfOutputTextField.text = _fileRef.nativePath.replace(".png",".atf");
_inputNativeProcess = _fileRef.nativePath;
_outputNativeProcess = _atfOutputTextField.text;
_flagLoadedFile = new Boolean(true);
var test:Bitmap = evt.target.data as Bitmap;
if(test){
trace(test.height);
}
_fileRef.removeEventListener(Event.SELECT, onFileSelected);
_fileRef.removeEventListener(ProgressEvent.PROGRESS, onProgress);
_fileRef.removeEventListener(Event.COMPLETE, onComplete);
_fileRef.removeEventListener(Event.CANCEL, onCancel);
Now, in that event i want to check my file size... I ve tried many things but didnt get success... and sometimes i get null from my _fileRef.data.
Any suggestions to solve that?
thx
Just to make sure, are you getting the data inside the onComplete handler? The code you show doesn't do that right now. Should be something like :
_fileRef.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
private function onComplete(e:Event):void
{
var test:Bitmap = e.target.data as Bitmap;
if(test)
trace(test.height);
}
the Answer is -
//add that on my public function onComplete(evt:Event):void{}
var loader:Loader = new Loader();
loader.loadBytes(byteArray);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
// build another event
publicfunction loaderComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
var bitmapData:BitmapData = new BitmapData(loaderInfo.width,
loaderInfo.height, false, 0xFFFFFF);
bitmapData.draw(loaderInfo.loader);
// result: bitmapData
}
now i can get the Heigh, witdh and whatever... thx!

as3 MouseDown Timer

I would like to know a way can detect on how long the mouse id down on the item, and like a checker, to check on long the mouse id been down on the specific item. is it possible to using timer?
Many Thanks
Sure, but you don't need a timer:
import flash.utils.getTimer;
var startTime:int = 0;
var endTime:int = 0;
function SomeFunctionOrClassConstructor():void
{
item.addEventListener(MouseEvent.MOUSE_DOWN, startMouseDown, false, 0, true);
}
function startMouseDown($evt:MouseEvent):void {
startTime = getTimer();
item.removeEventListener(MouseEvent.MOUSE_DOWN, startMouseDown);
item.addEventListener(MouseEvent.MOUSE_UP, endMouseDown, false, 0, true);
}
function endMouseDown($evt:MouseEvent):void {
endTime = getTimer();
item.addEventListener(MouseEvent.MOUSE_DOWN, startMouseDown, false, 0, true);
item.removeEventListener(MouseEvent.MOUSE_UP, endMouseDown);
trace(endTime - startTime);
}
That should get you started.