I am trying to click on a button and trace out a number "1" onto the stage/same scene.
I have this code:
button1.addEventListener(MouseEvent.CLICK, myClickReaction1);
function myClickReaction1 (e:MouseEvent):void{
trace("1");
}
BUT it traces onto the output of flash and not onto the scene.
please help
thanks
trace("1"); will go to the output panel it is intended for debugging.
If you want to see something on the stage, you will need to create a TextField and set it's text property to whatever you want.
button1.addEventListener(MouseEvent.CLICK, myClickReaction1);
function myClickReaction1 (e:MouseEvent):void{
var tf:TextField = new TextField();
tf.text = "1";
addChild(tf);
}
Related
first of all, I'm a total noob to as3 and coding in general, I barely operate outside of code snippets.
I'm working on a project, and part of which is a scene where you get a custom mouse cursor upon entering the scene, and when you leave the scene, the custom mouse cursor is removed. The code I'm using to start the custom cursor is:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
with crsTemple being the instance name for the custom cursor. Then, when a new scene is entered (via rolling over an object), i have the following code in the new scene:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
function fl_CustomMouseCursor_4(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
crsTemple.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
stage.removeChild(crsTemple);
Mouse.show();
Unfortunately, whenever I go into the second scene, I get the regular mouse again, but it drops the crsTemple wherever the mouse was when the scene change happened, and it stays there for the rest of the time the file is running.
Any help is greatly appreciated, much thanks in advance for helping a noob like me!
No need to write the same code in new Scene. You can actually use all declarations form the first Scene. In the following code snippet MOUSE_MOVE handler (fl_CustomMouseCursor) from scene 1 will be called in scene 2 either. Custom cursor will also be accessible by its name crsTemple.
import flash.display.MovieClip;
import flash.events.MouseEvent;
var crsTemple:Sprite = new CrsTemple();
crsTemple.mouseEnabled = false;
addChild(crsTemple);
// for smooth cursor movement MOUSE_MOVE instead of ENTER_FRAME
stage.addEventListener(MouseEvent.MOUSE_MOVE, fl_CustomMouseCursor);
stage.addEventListener(MouseEvent.CLICK, nextStage); // for test purpose, just to switch the stage
function fl_CustomMouseCursor(event:Event):void
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
trace(crsTemple.x);
}
function nextStage(e:Event):void {
gotoAndStop(1,"Scene 2");
}
Mouse.hide();
stop();
here is a link to fla sample
I'm trying to create a mute and unmute button. I don't want any play/pause button just mute and unmute. So far all I've created is the mute button, the problem is I have no clue as to make an unmute button. I'd also like the mute icon to change its appearance to unmute icon once it's clicked. Any help is greatly appreciated, thanks everyone.(some side comments or explanations would be nice too)
Hope I'm not asking for too much. Here's my code so far
var soundReq:URLRequest = new URLRequest("assets/for_flash.mp3");
var sound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete (e:Event):void
{
sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteSound);
function muteSound(e:MouseEvent):void
{
SoundMixer.stopAll();
}
Let's assume in your mute_btn instance, you have a child icon with an instance name of muteIcon and the presence of that icon is a visual indicator on whether your sound is presently unmuted or not (Like a circle with a slash through it type icon) and underneath that is a speaker icon.
This would be code then:
//First, you need to assign the result of the sound.play function to the sound channel
function onComplete (e:Event):void{
channel = sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteBtnClick);
function muteBtnClick(e:MouseEvent):void
{
//check to see if the sound channel exists yet (in case you click the button before the sound loads, otherwise you'll get an error
if(!channel) return;
//toggle the icon's visibility
mute_btn.muteIcon.visible = !mute_btn.muteIcon.visible;
//now check and see if the icon is visible or not
if(mute_btn.muteIcon.visible){
//it is visible, so we should unmute the sound
channel.soundTransform = new SoundTransform(1); //1 is the volume level from 0 - 1
}else{
//it is not visible, so we should mute the sound
channel.soundTransform = new SoundTransform(0); //0 is no volume, 1 is full volumn
}
}
Also, for efficiency, you can change this:
var channel:SoundChannel = new SoundChannel();
To this:
var channel:SoundChannel;
Because creating a new object in that var is pointless as a whole new object gets assigned when you do channel = sound.play();
So I'd like to set this up to where you click on a button it loads a new scene and unloads the previous scene.
This is what I have so far.
staart.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
function loadSWF(swfURL){
var myLoader:Loader = new Loader();
var mySWF:URLRequest = new URLRequest(swfURL);
myLoader.contentLoaderInfo.addEventListener
(Event.COMPLETE,onCompleteHandler);
myLoader.contentLoaderInfo.addEventListener
(ProgressEvent.PROGRESS,onProgressHandler);
myLoader.load(mySWF);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.currentTarget.content);
loadEvent.currentTarget.content.gotoAndStop(swfFrame);
}
function onProgressHandler(myProgress:ProgressEvent){
var percent:Number =
Math.round(myProgress.bytesLoaded/myProgress.bytesTotal*100);
trace(percent+"% loaded");
}
}
var swfFrame:Number= 1;
loadSWF("home.swf");
}
Is it possible to unload a specific swf file or previous swf file through a newly loaded swf file?
You should create a variable for your swf.
Add this to your code :
var mc:MovieClip = new MovieClip();
// Adds the movie clip at initialization.
addChild(mc);
and modify your onCompleteHandler method like this :
function onCompleteHandler(loadEvent:Event){
// Changes the content of your movie clip.
mc = loadEvent.currentTarget.content;
mc.gotoAndStop(swfFrame);
}
shawn gave you the right answer but there are 2 other problems in your code:
There's a typo in element staart and a potential memory leak because you add event listeners without removing them. You should add the following two lines in onCompleteListener function:
loadEvent.currentTarget.removeEventListeners(Event.COMPLETE,onCompleteHandler)
loadEvent.currentTarget.removeEventListeners(ProgressEvent.PROGRESS,onProgressHandler)
If you don't remove the listeners, the garbage collector will be unable to free the memory of every loader you create on each click
So i'm developing an application in which allows users to Type on the stage. The text needs to appear wherever the mouse cursor is positioned (and the text would follow the mouse cursor if moved). For the most part it works pretty well. I have two questions.
1) How would I allow the text to follow the mouse cursor? My current code only allows users to type when they click down on the stage.
var textfield = new TextField();
textfield.type = TextFieldType.INPUT
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.defaultTextFormat = textformat;
textfield.textColor = selectedColor;
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
textfield.selectable = false;
/* Add text to stage*/
board.addChild(textfield);
My second question, how would I clear the text? The text would be "pasted" down onto the canvas, so using the "" method would not work. I want an "eraser" to erase the text from the stage. It would be just like stage.graphics.clear but just for text.
Any help I could get would be greatly appreciated. I am still very new to AS3 and I think I gave all the information needed. Thanks.
To make your text follow the mouse cursor, you can add an event listener to the stage:
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
function onMouseMove(event:MouseEvent){
textfield.x = event.stageX;
textfield.y = event.stageY;
}
to make it stop following, you can remove the event listener:
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
to clear the text, you call textfield.text = "";
I'm working on a simple image processor in AS3 and as usual I almost had it finished when an annoying little problem appeared. I want to open an image using a dialog box, and once the image is open I want the "selectBtn" (which is in my Library) to disappear. However when I try the code below I get a: "Error #1009: Cannot access a property or method of a null object reference". I'm sure it's something simple but I just can't figure it out!
// declare variables
var image:Bitmap;
var loader:Loader = new Loader();
var fileRef:FileReference= new FileReference();
selectBtn.addEventListener(MouseEvent.CLICK, openImage);
function openImage(event:MouseEvent):void {
fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
fileRef.addEventListener(Event.SELECT, onFileSelected);
}
function onFileSelected(e:Event):void {
fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
fileRef.load();
}
function onFileLoaded(e:Event):void {
loader.loadBytes(e.target.data);
image = Bitmap(loader.content);
selectBtn.visible = false;
}
What Todd says is accurate: where is "selectBtn" defined? Is it on a stage, or nested within some movieclip instance?
Assuming the button exists on the stage, and the code runs in the same frame as the button exists, you could try this.stage.selectBtn.visible = false;
Otherwise, trace that you have selectBtn as a reference. Or you could declare a variable to reference it. i.e.:
var selectBtnRef:Button = this.stage.selectBtn;