AS3 Mouse Over Change Image - actionscript-3

Currently I'm trying to change image loaded in a sprite with mouseover event and change it back with mouseout. But it's not working correctly, am i missing something?
public class Tab extends Sprite
{
var imageLoader:Loader = new Loader();
var TabSprite:Sprite = new Sprite();
var SkinImages:Array = [Skin.TAB_ACTIVE,Skin.TAB_DISABLED,Skin.TAB_HOVER,Skin.TAB_VIEW];
public function Tab()
{
for each (var Image:String in SkinImages){
imageLoader.load(new URLRequest(Image));
}
TabSprite.buttonMode = true;
addChild(TabSprite);
TabSprite.addChild(imageLoader);
TabSprite.addEventListener(MouseEvent.MOUSE_OVER, onTabHover);
}
private function onTabHover(e:MouseEvent){
trace("HOVER");
TabSprite.removeEventListener(MouseEvent.MOUSE_OVER, onTabHover);
imageLoader.load(new URLRequest(Skin.TAB_HOVER));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
TabSprite.addEventListener(MouseEvent.MOUSE_OUT, onTabOut);
});
}
private function onTabOut(e:MouseEvent){
trace("OUT");
TabSprite.removeEventListener(MouseEvent.MOUSE_OUT, onTabOut);
imageLoader.load(new URLRequest(Skin.TAB_VIEW));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
TabSprite.addEventListener(MouseEvent.MOUSE_OVER, onTabHover);
});
}
}

You shouldn't nest listeners that way. Just add two in the constructor:
TabSprite.addEventListener(MouseEvent.ROLL_OVER, onTabHover);
TabSprite.addEventListener(MouseEvent.ROLL_OUT, onTabOut);
Note changing MOUSE_OVER to ROLL_OVER it's better in most cases. You shouldn't also load images at every mouse event. Preload them, and then use. Also using anonymous functions in listeners is bad practice as you are not able to remove that listener:
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
TabSprite.addEventListener(MouseEvent.MOUSE_OUT, onTabOut);
});
And in fact you are not removing it - this is bad.
for each (var Image:String in SkinImages){
imageLoader.load(new URLRequest(Image));
}
I doubt it works, I think you cannot load many images at once by using one loader.
Try this:
public class Tab extends Sprite
{
var imageOverLoader:Loader = new Loader();
var imageOutLoader:Loader = new Loader();
var TabSprite:Sprite = new Sprite();
var SkinImages:Array = Skin.TAB_ACTIVE,Skin.TAB_DISABLED,Skin.TAB_HOVER,Skin.TAB_VIEW];
public function Tab()
{
TabSprite.buttonMode = true;
this.addChild(TabSprite); // you also need to add as a Child "Tab" object in the Main Class
imageOutLoader.load(new URLRequest(Skin.TAB_VIEW));
imageOverLoader.load(new URLRequest(Skin.TAB_HOVER));
TabSprite.addChild(imageOutLoader);
TabSprite.addEventListener(MouseEvent.ROLL_OVER, onTabHover);
TabSprite.addEventListener(MouseEvent.ROLL_OUT, onTabOut);
}
private function onTabHover(e:MouseEvent){
TabSprite.removeChild(imageOutLoader);
TabSprite.addChild(imageOverLoader);
trace("HOVER");
}
private function onTabOut(e:MouseEvent){
TabSprite.removeChild(imageOverLoader);
TabSprite.addChild(imageOutLoader);
trace("OUT");
}
}
Try this.

Related

ActionScript3: removeEventListener with function parameter

how can i remove EventListener from MovieClip in actionscript? Below is my sample code.
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
var color: ColorTransform = new ColorTransform();
color.color = 0x00CC66;
colorChange_mc.buttonMode = true;
colorChange_mc.addEventListener(MouseEvent.MOUSE_OVER, changeColor(color));
colorChange_mc.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
function changeColor(color: ColorTransform): Function {
return function (e: MouseEvent): void {
colorChange_mc.transform.colorTransform = color;
}
}
function changeColorToNormal(e: MouseEvent): void {
var color: ColorTransform = new ColorTransform();
color.color = 0x000033;
colorChange_mc.transform.colorTransform = color;
}
changer_mc.buttonMode = true;
changer_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeEvent);
function removeEvent(e: MouseEvent): void {
colorChange_mc.removeEventListener(MouseEvent.MOUSE_OVER, changeColor(color));
}
I created two MovieClips on the stage , colorChange_mc has two EventListeners one for mouseDown and one for mouseOut. When change_mc button is pressed I want to remove one of the EventListener. I was able to remove EventListener without passing parameter to changeColor function. But in my real class this parameter plays crucial role.
Use a class-var instead - one you have already in your code. To keep straight I created a second ColorTransform as a class-var - normalColor
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
var color: ColorTransform = new ColorTransform();
color.color = 0x00CC66;
var normalColor: ColorTransform = new ColorTransform();
normalColor.color = 0x000033;
colorChange_mc.buttonMode = true;
colorChange_mc.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
colorChange_mc.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
function changeColor(e:MouseEvent): void{
colorChange_mc.transform.colorTransform = color;
}
function changeColorToNormal(e:MouseEvent): void {
colorChange_mc.transform.colorTransform = normalColor;
}
changer_mc.buttonMode = true;
changer_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeEvent);
function removeEvent(e:MouseEvent): void {
colorChange_mc.removeEventListener(MouseEvent.MOUSE_OVER, changeColor);
}
Greetings.
The problem is that in order to remove the listener, you have to identify the function that you originally passed to it. But you have no reference to the function, because it is anonymous and lives in the global scope.
Think object oriented. Put all the functionality of changing the color into a class. All you actually want to do is
specify a color value
be able to disable the whole thing again.
Add two methods to the class of colorChange_mc that let you do this:
public function set color(value:uint):void
and
public function set disable(value:Boolean):void
Alternatively, you can override the enabled setter function to tie your logic to it, if this is something you desire.
This way you can refactor most of the code that currently bloats your main time line into the class.
According to my first answer and your comment with new facts here is a solution for more buttons/colors using a Dictionary...
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.utils.Dictionary;
import flash.display.MovieClip;
var normalColor: ColorTransform = new ColorTransform();
normalColor.color = 0x000033;
//button1-30 are the instancenames of your buttons
var initObj:Array=[{btn:button1, color:0xFFFFFF},
{btn:button2, color:0xFF0000},
{btn:button3, color:0x00FF00},
...
{btn:button30, color:0x000000},
];
var dict:Dictionary = new Dictionary();
initButtons();
function initButtons():void
{
for each (var item:Object in initObj)
{
var btn = item.btn;
btn.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
btn.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
dict[btn] = {color:item.color};
}
}
function changeColor(e:MouseEvent):void
{
var btn:MovieClip = e.target as MovieClip;
var color:uint = dict[btn].color;
var cT:ColorTransform=new ColorTransform();
cT.color = color;
colorChange_mc.transform.colorTransform = cT;
}
function changeColorToNormal(e:MouseEvent):void
{
colorChange_mc.transform.colorTransform = normalColor;
}
Greetings.
Thank you for your replies. As I said before the code that i posted above was just an example. Sorry if I asked unclear question. I solved my problem by disabling some specific buttons when colorChange button is pressed.
_userCardsButtons_mc.mouseEnabled = false;
_userCardsButtons_mc.mouseChildren = false;
_userCardsButtons_mc.buttonMode = false;
this code disabled event listeners that I added before.

actionScript3 ENTER_FRAME Event is not working

I am trying to to use the ENTER_FRAME Event on playing audio ,but the code is not executing the handler function.
public function play_mp3(path:String,path1:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
addEventListener(Event.ENTER_FRAME,myFunction);}
public function myFunction(e:Event){
LogFB.info('test1234'); }
You're issue is likely that the class whose code you've posted, is not on the display tree. (it's not a display object or hasn't been added to the stage).
If it's not on the display tree, then ENTER_FRAME will not dispatch on it.
You can get around this a few ways.
Pass into the class a reference to something that is on the stage (or the stage itself). Then add the ENTER_FRAME listener on that object.
var stage:Stage;
public function MyClass(stage_:Stage){
stage = stage_;
}
....
stage.addEventListener(Event.ENTER_FRAME, myFunction);
Forgo ENTER_FRAME, and just use a Timer
var timer:Timer
public function play_mp3(path:String,path1:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
timer = new Timer(250); //tick every quarter second
timer.addEventListener(TimerEvent.TIMER, myFunction);
timer.start();
}
public function myFunction(e:Event){
LogFB.info('test1234');
}
Your problem is in LogFB.
Try this and you will see the trace from inside the function...
var channel : SoundChannel = new SoundChannel();
function play_mp3(path:String,path1:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
addEventListener(Event.ENTER_FRAME,myFunction);
}
function myFunction(e:Event){
//LogFB.info('test1234'); here is the problem
// trace("is working!");
hi();
}
play_mp3('aasa','aaa');
function hi() {
trace("goooooddddd!"); //is working, I am using Flash CC
}
All the best!
there doesnt seem any error try this one
public function play_mp3(path:String,path1:String):void {
addEventListener(Event.ENTER_FRAME,myFunction);
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
}
public function myFunction(e:Event){
LogFB.info('test1234'); }
ie put the enterframe event the first thing that hapen in your code to check if at least it initialise it?

How do I unload an external ".swf" file to load another?

I am a student who's working for months on a game and now I've got stuck on a problem.
I am new to actionscript 3 but i learn fast.
I can load my menu screen ("startScreen.swf") into my game (firstgame) automatically and everything works fine when I click play or storyScreen or instructionsScreen. But the problem is that I want to return after pressing ESC button. I have tried many code but nothing works exactly how I want.
Example: I click on story and the swf (storyScreen.swf) loads and I can read the story and when I am finished, I want to press ESC to reload my swf (startScreen.swf) and all the functions in it. Like play and instructions.
You can find my code and empty space in the function (esc).
I know it is maybe easy to solve but I really don't know how. :(
public class FirstGame extends MovieClip
{
public var Player:MovieClip
private var leftKeyIsDown:Boolean;
private var RightKeyIsDown:Boolean;
private var aMissileArray:Array;
private var aEnemyArray:Array;
public var scoreTxt:TextField;
public var ammoTxt:TextField;
public var MenuEnd:EndGameScreen;
public var menuAgain:EndGameScreen;
private var MenuStart:mcStartGameScreen;
private var MenuStory:mcStartGameScreen;
private var MenuInstructions:mcStartGameScreen;
private var nScore:Number;
private var nAmmo:Number;
private var tEnemyTimer:Timer;
public function FirstGame()
{
//Create a loader object
var startLoader:Loader = new Loader();
//add event listener to listen for the complete event
startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
//load our loader object
startLoader.load(new URLRequest("startScreen.swf"));
}
public function startLoaded(e:Event):void
{
MenuEnd.hideScreen();
Player.visible = false;
scoreTxt.visible = false;
ammoTxt.visible = false;
//get a reference to the loaded movieclip
MenuStart = e.target.content as mcStartGameScreen;
//listen for start game event
MenuStart.addEventListener("START_GAME", playGameAgain);
//add it to the stage
addChild(MenuStart);
//get a reference to the loaded movieclip
MenuStory = e.target.content as mcStartGameScreen;
//listen for start game event
MenuStory.addEventListener("SHOW_STORY", storyGameScreen);
//add it to the stage
addChild(MenuStory);
//get a reference to the loaded movieclip
MenuInstructions = e.target.content as mcStartGameScreen;
//listen for start game event
MenuInstructions.addEventListener("SHOW_INSTRUCTIONS", instructionsGameScreen);
//add it to the stage
addChild(MenuInstructions);
}
private function instructionsGameScreen(e:Event):void
{
var instructionsLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("instructionsScreen.swf");
instructionsLoader.load(url);
addChild(instructionsLoader);
stage.addEventListener(KeyboardEvent.KEY_UP, esc);
}
private function storyGameScreen(e:Event):void
{
var storyLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("storyScreen.swf");
storyLoader.load(url);
addChild(storyLoader);
stage.addEventListener(KeyboardEvent.KEY_UP, esc);
}
private function esc(e:KeyboardEvent):void
{
if (e.keyCode == 27)
{
//fscommand("quit");
/////////test//////////////////(new URLRequest(stage.loaderInfo.url), "FirstGame.swf");
}
}
private function playGameAgain(e:Event):void
{
//initialize variables
aMissileArray = new Array();
aEnemyArray = new Array();
nScore = 0;
nAmmo = 20;
Player.x = 262,95
Player.y = 323,30
Player.visible = true;
scoreTxt.visible = true;
ammoTxt.visible = true;
MenuStart.hideScreen();
MenuEnd.addEventListener("PLAY_AGAIN", playGameAgain);
MenuEnd.hideScreen();
updateScoreText();
updateAmmoText();
//trace("First Game Loaded");
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
stage.addEventListener(Event.ENTER_FRAME, gameLoop)
//creat an timer object
tEnemyTimer = new Timer(1000)
//listen for the timer ticks/intervals
tEnemyTimer.addEventListener(TimerEvent.TIMER, addEnemy)
//start our timer
tEnemyTimer.start();
}
I believe you should be able to start a new request on the same loader object. Either way, you're dealing with object creation + cleanup. The crux of the solution I'm offering is that you reuse your loaders.
Your current code is somewhat repetitious, so I've modified it slightly to demonstrate how you could simplify the code. Some thoughts while reviewing your code:
You're adding the same object to the stage multiple times (ie.,
MenuStart, MenuStory, MenuInstructions); these are all pointers to
the same root swf you loaded (a.k.a., startLoader).
You've registered events the stage at multiple locations. Best
practice is to place these in your constructor as they are
persistent.
Because you want to reuse your loaders at a later point, keeping a
table with them makes it easier to reference.
Any time you find yourself programming the same code in similar ways,
it's a good indication that you can simplify with a single function
(simply change the arguments).
Give this a try:
public var Player:MovieClip
private var leftKeyIsDown:Boolean;
private var RightKeyIsDown:Boolean;
private var aMissileArray:Array;
private var aEnemyArray:Array;
public var scoreTxt:TextField;
public var ammoTxt:TextField;
public var MenuEnd:EndGameScreen;
public var menuAgain:EndGameScreen;
private var MenuStart:mcStartGameScreen;
private var MenuStory:mcStartGameScreen;
private var MenuInstructions:mcStartGameScreen;
private var nScore:Number;
private var nAmmo:Number;
private var tEnemyTimer:Timer;
private var screens:Object = {
"SHOW_INSTRUCTIONS":{
"loader":new Loader(),
"url":new URLRequest("instructionsScreen.swf")
},
"SHOW_STORY":{
"loader":new Loader(),
"url":new URLRequest("storyScreen.swf")
},
"START_GAME":{
"loader":new Loader(),
"url":new URLRequest("startScreen.swf")
}
}
public function FirstGame() {
var startLoader:Loader = loadScreen({"type":"START_GAME"});
//Register our event listeners
startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
stage.addEventListener(KeyboardEvent.KEY_UP, esc);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(Event.ENTER_FRAME, gameLoop)
}
private function loadScreen(e:Object):Loader {
screens[e.type].loader.load(screens[e.type].url);
addChild(screens[e.type].loader);
return screens[e.type].loader;
}
public function startLoaded(e:Event):void {
//initialize variables
aMissileArray = new Array();
aEnemyArray = new Array();
nScore = 0;
nAmmo = 20;
Player.x = 262, 95
Player.y = 323, 30
Player.visible = true;
scoreTxt.visible = true;
ammoTxt.visible = true;
MenuEnd.addEventListener("PLAY_AGAIN", playGameAgain);
MenuEnd.hideScreen();
updateScoreText();
updateAmmoText();
//creat an timer object
tEnemyTimer = new Timer(1000)
tEnemyTimer.addEventListener(TimerEvent.TIMER, addEnemy)
tEnemyTimer.start();
var swfRoot = screens.START_GAME.loader["content"];
swfRoot.addEventListener("START_GAME", loadScreen);
swfRoot.addEventListener("SHOW_STORY", loadScreen);
swfRoot.addEventListener("SHOW_INSTRUCTIONS", loadScreen);
}
private function esc(e:KeyboardEvent):void {
if (e.keyCode == 27) {
//fscommand("quit");
// Loop through our loaders, and reset them all.
for each (var entry:Object in screens) {
entry.loader.unloadAndStop();
removeChild(entry.loader);
}
// Reload the start screen
var startLoader:Loader = loadScreen({"type":"START_GAME"});
startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
}
}
The last thing I want to be guilty of is telling you how to run your project, however, I think you may be taking the wrong approach, here.
Instead of setting up the story and the game as separate swf files, what you would probably need to do is set up the story as a MovieClip, and the game as a MovieClip. (Yes, MovieClips can contain other symbols, including other MovieClips.) Then, you'll want to hide/show or add/remove these MovieClips from your stage using code.
If you need to know how to do this, let me give you the age-old admonishment: RTD (Read the Documentation). These are basic tasks that are covered both in the online Adobe Flash documentation, and across numerous tutorials online.
I hope that helps!

Register some properties in event handling

I want to show several images to my application
sourceAudioFile =(event.target as File);
sourceAudioFile.data;
var myLoader:Loader = new Loader();
var url :URLRequest = new URLRequest("file:///" + sourceAudioFile.nativePath);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
myLoader.load(url);
Registration Listener
private function onImageLoaded(e:Event):void
{
var image : Bitmap= new Bitmap(e.target.content.bitmapData);
image.scaleX = 0.5 ;
image.scaleY = 0.5 ;
}
Is there any way by which i can add some custom values to Event.COMPLETE to know what type of image is being loaded.
like e.imageName , e.noOfImage which i can access in complete handler & provide values to it when i register this event.
Thanks in advance
Yes you can, try registering event like:
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event){
dispatchEvent(new CustomEvent(CustomEvent.COMPLETE, "<imageType>"));
}, false, 0, true);
And Add new Class, extending the Event class:
public class CustomEvent extends Event {
public static const COMPLETE:String = "CustomEventComplete";
public var imageType:String = "";
public function CustomEvent(type:String, imageType:String = "", bubbles:Boolean = false, cancelable:Boolean = false){
super(type, bubbles, cancelable);
this.imageType = imageType;
}
override public function clone():Event {
return new CustomEvent(type, customTarget, bubbles, cancelable);
}
}
Now you can register for CustomEvent.COMPLETE & get the image type in the listener.
You can find out what ahs been loaded by checking LoaderInfo's url property
private function onImageLoaded(e:Event):void
{
var cli:LoaderInfo = e.target as LoaderInfo;//you added listener to the contentLoaderInfo property
trace(cli.url);//url of the media being loaded
//do something with it
}
also in this SO question you can get more sophisticated asnwer to your problem:)
pass arguments to event listener
Yes, there's a simple way:
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded(imageName, noOfImage));
myLoader.load(url);
function onImageLoaded(imageName:String, noOfImage:int):Function {
return function(e:Event):void {
// Respective "e", "imageName" and "noOfImage" are available for you here.
}
}
If you need to remove it later, change the first line to:
var functionOnImageLoaded:Function = onImageLoaded(imageName, noOfImage);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, functionOnImageLoaded);
Then later just do it like this:
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, functionOnImageLoaded);
I've answered this before. See it here.

Making loaded image draggable actionscript 3

i want to load an image to stage using Loader and then I want to make it draggable. Loading is done by selecting from tilelist as seen below, but I have no idea about drag and drop in AS3, can anybody help me? I want to make it as simple as possible.
Here is my code to load image:
var charge1:Loader = new Loader();
addChild(charge1);
this.addChildAt(charge1, getChildIndex(bg));
charge1.x = 280;
charge1.y = 270;
...
function setCharge1(e:Event):void{
trace(e.target.selectedItem.source);
this.charge1.load(new URLRequest(e.target.selectedItem.source));
this.charge1.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
}
yuku was going along the right lines but what you want to do is get the contents of the loaderInfo which is actually the clip that is loaded in. something like
private function onComplete(event : Event) : void
{
var loadedClip : MovieClip = LoaderInfo(event.target).content;
loadedClip.addEventListener(MouseEvent.MOUSE_DOWN, function(event : MouseEvent)
{
loadedClip.startDrag();
});
stage.addEventListener(MouseEvent.MOUSE_UP, function(event : MouseEvent)
{
loadedClip.stopDrag();
});
}
This is the easiest way to me...
/* Load Image */
var myLoader:Loader = new Loader();
imageContainer.addChild(myLoader);
var url:URLRequest = new URLRequest("photo.jpg");
myLoader.load(url);
/* Drag and Drop */
imageContainer.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
function pickUp(event:MouseEvent):void
{
imageContainer.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, dopIt);
function dopIt(event:MouseEvent):void
{
imageContainer.stopDrag();
}
Use the following code:
this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
this.addEventListener(MouseEvent.MOUSE_UP, dropMovie);
this.buttonMode = true;
private function dragMovie(event:MouseEvent):void
{
this.startDrag();
}
private function dropMovie(event:MouseEvent):void
{
this.stopDrag();
}
Loader is a subclass of Sprite, so you can use startDrag and stopDrag method.
For example:
charge1.addEventListener("mouseDown", function() {
charge1.startDrag();
});
stage.addEventListener("mouseUp", function() {
charge1.stopDrag();
});