I am attempting to load multiple images in AS3 and I'm exploring different options on how this can be done.
I would like to have to only use one Load() instance and handle the various image assignments in the onComplete handler...
here is my first attempt:
var buttonLdr:Loader = new Loader();
buttonLdr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
buttonLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
whichButton = 0;
buttonLdr.load(new URLRequest("images/test.png"));
whichButton = 1;
buttonLdr.load(new URLRequest("images/testDown.png"));
whichButton = 2;
buttonLdr.load(new URLRequest("images/testHover.png"));
private function onLoadComplete(e:Event):void
{
switch(whichButton)
{
case 0:
buttonBgUp = e.target.loader.content;
break;
case 1:
buttonBgDown = e.target.loader.content;
break;
case 2:
buttonBgHover = e.target.loader.content;
break;
}
create();
}
A pretty straightforward approach... although it only calls the function on the last call, setting the buttonBgHover, but nothing else.
Any guidance to what might be going on here would be greatly appreciated.
-thanks
You need to call next load only when previous load completes.
whichButton = 0;
buttonLdr.load(new URLRequest("images/test.png"));
private function onLoadComplete(e:Event):void
{
switch(whichButton)
{
case 0:
buttonBgUp = e.target.loader.content;
whichButton = 1;
buttonLdr.load(new URLRequest("images/testDown.png"));
break;
case 1:
buttonBgDown = e.target.loader.content;
whichButton = 2;
buttonLdr.load(new URLRequest("images/testHover.png"));
break;
case 2:
buttonBgHover = e.target.loader.content;
break;
}
create();
}
Related
I have a sprite in my library called myRocket but it won't show up.
I made my sprite a movie clip then I deleted it off the actual page so it was just in the library. I thought using this code, the sprite would appear once I started up the game but it does not.
import flash.display.MovieClip;
stop() ;
removeChild(myButton);
var myReturn:Return=new Return();
addChild(myReturn);
myReturn.x=390;
myReturn.y=10;
myReturn.addEventListener(MouseEvent.CLICK, return1Function);
var up:Boolean;
var down:Boolean;
var left:Boolean;
var right:Boolean;
var speed:int;
function return1Function(evt:MouseEvent):void{
gotoAndStop("menu");
}
var myRocket:MovieClip;
addChild(myRocket);
myRocket.x=200;
myRocket.y=150;
function KeyboardDemo() {
myRocket.x = 200;
myRocket.y = 100;
addChild(myRocket);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
}
function keyPressedDown(event:KeyboardEvent):void {
var key:uint = event.keyCode;
var step:uint = 5
switch (key) {
case Keyboard.LEFT :
myRocket.x -= step;
break;
case Keyboard.RIGHT :
myRocket.x += step;
break;
case Keyboard.UP :
myRocket.y -= step;
break;
case Keyboard.DOWN :
myRocket.y += step;
break;
}
}
You need to initialize your object before you place it to the stage. You have only declared a variable at the line "var myRocket:MovieClip;" instead it should at least (not sure about whatever logic you have there" read like above with "Return" whatever it was: var myRocket:MovieClip=new Rocket(); Here "Rocket" is the library name of the sprite.
I was working on a 'as' file and then ctrl+entered the project. Only thing I see is empty scene. The earlier test was fully functional and visual. Is there anything about 'fla' size or so?
There is a mouse event at the very beginning and it does not go to the event function.
public function Game()
{
// constructor code
init(0);
}
private function init(level:int):void
{
switch (level)
{
case 0 :
pis = new Yesil;
yldz_bir.addEventListener(MouseEvent.CLICK, gotoFirst, false, 0, true);
yldz_bir.buttonMode = true;
break;
case 1 :
pis = new Yesil;
item_speed = 3;
_puan = 0;
initialMain();
point.texts.text = "0";
can = 4;
main();
break;
}
}
Hi I made this code and I use flash cs5.5
var cijfer_txt:int = parseInt(textarea_text.text);
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
submit.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler_2);
function fl_TapHandler_2(event:TouchEvent):void
{
switch (cijfer_txt){
case 1:
gotoAndStop(12);
break;
case 2:
gotoAndStop(23);
break; }
};
but I don't get it why it isn't working, the animation has to go to frame 12 when I fill in "1" and stop and has to got to frame 23 if I fill in "2" and stop but he doesn't do it and I get sick of it!!
Try to set cijfer_txt in fl_TapHandler_2
function fl_TapHandler_2(event:TouchEvent):void
{
cijfer_txt = parseInt(textarea_text.text);
switch (cijfer_txt){
}
}
I think you have problem with debugging so I'll help you:
First: change your code as follows -
var cijfer_txt:int;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
submit.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler_2);
function fl_TapHandler_2(event:TouchEvent):void
{
cijfer_txt = parseInt(textarea_text.text);
trace("in the function, cijfer_txt = "+cijfer_txt);
switch (cijfer_txt) {
case 1:
trace("in case 1");
gotoAndStop(12);
break;
case 2:
trace("in case 2");
gotoAndStop(23);
break;
default:
trace("in defaukt");
break;
}
}
NOW RUN IT AND WATCH THE CONSOLE\OUTPUT FOR TRACE OUTPUTS,
according to the output you can see what is hapening!
trace(); is a very common method
Good Luck
(Don't forget to mark as accepted if it helped you!)
Why cant i connect? When i run the application i receive NetConnection.failed in the output. Can you please take a carefull look at my code. I believe my string url is correct. I am unsure is my FMS is down or if theres a code error.
Thank You
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.NetStream;
import flash.media.Camera;
import flash.media.Video;
import flash.media.Microphone;
import fl.controls.Button;
var nc:NetConnection;
var ns:NetStream;
var cam:Camera;
var microphone:Microphone;
var vid:Video;
var connectButton:Button;
//Connecting to Flash Media Server
nc = new NetConnection();
nc.connect("rtmfp:/fms/streamExample");
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
function netHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
{
connectButton.label = "Disconnect";
connectButton.enabled = true;
sendButton.enabled = true;
onConnect();
break;
}
case "NetConnection.Connect.Closed":
{
connectButton.label = "Connect";
connectButton.enabled = true;
sendButton.enabled = false;
nc.close();
break;
}
case "NetConnection.Connect.Failed":
{
trace("Sorry your connection failed");
break;
}
case "NetConnection.Connect.Rejected":
{
trace("Oops the connection has been rejected");
break;
}
}
}
ns = new NetStream(nc);
ns.publish("live", "viewing");
ns.attachCamera();
//Setting the video
var vid:Video = new Video();
vid.height = cam.height;
vid.width = cam.width;
vid.x = 10;
vid.y = 10;
addChild(vid);
cam = Camera.getCamera();
cam.setMode(400, 300, 20);
cam.setQuality(0, 85);
cam.setKeyFrameInterval(18);
vid.attachCamera();
addChild(cam);
//Setting the audio
microphone = Microphone.getMicrophone();
microphone.codec = SoundCodec.SPEEX;
microphone.framesPerPacket = 1;
microphone.setSilenceLevel(0, 200);
microphone.gain = 50;
NetStream.attachAudio(microphone);
If your Flash Media Server is on the same machine than the one your testing your application from, you should change the URL of server with rtmfp://localhost/streamExample
If your server is on another machine, then you just have to replace localhost with the IP address of the machine running the server.
You should also add the event listener on your NetConnection before you perform the connect() call.
To help you known the reason of the failure, you might want to trace the description of your event as well.
Here is a sample with all these indications:
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
nc.connect("rtmfp:/localhost/streamExample");
function netHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
{
connectButton.label = "Disconnect";
connectButton.enabled = true;
sendButton.enabled = true;
onConnect();
break;
}
case "NetConnection.Connect.Closed":
{
connectButton.label = "Connect";
connectButton.enabled = true;
sendButton.enabled = false;
nc.close();
break;
}
case "NetConnection.Connect.Failed":
{
trace("Sorry your connection failed");
trace("Reason is: " + event.info.description);
break;
}
case "NetConnection.Connect.Rejected":
{
trace("Oops the connection has been rejected");
break;
}
}
}
I am currently working on a game where you need to survive as long as possible while dodging questions that come your way. (all with AS3)
At the moment I am going from 1 scene to another in between the game field and the question field, but everytime I go to the question scene the timer in the game scene resets itself. I was wondering if it was possible to have the timer continue while being in the question scene?
Also I have a movable character in between the menus which incidentally are also made in different scenes and the player is able to move him around, and I would very much like him to stay in the last position he was in the next screen, as in I move him to the top right in the main menu and when I go to the options menu I want him to still be in the top right and not in his initial position.
As for my timer this is the code I am using at the moment:
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.DateTimeFormatter;
var timer:Timer = new Timer(100);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 0;
function timerTickHandler(Event:TimerEvent):void
{
timerCount += 100;
toTimeCode(timerCount);
}
function toTimeCode(milliseconds:int) : void {
//create a date object using the elapsed milliseconds
var time:Date = new Date(milliseconds);
//define minutes/seconds/mseconds
var minutes:String = String(time.minutes);
var seconds:String = String(time.seconds);
var miliseconds:String = String(Math.round(time.milliseconds)/100);
//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
minutes = (minutes.length != 2) ? '0'+minutes : minutes;
seconds = (seconds.length != 2) ? '0'+seconds : seconds;
//display elapsed time on in a textfield on stage
timer_txt.text = minutes + ":" + seconds+"." + miliseconds;
}
And my character is using this code:
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
rutte.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
rutte.y -= 5;
}
if (downPressed)
{
rutte.y += 5;
}
if (leftPressed)
{
rutte.x -= 5;
rutte.scaleX = 1; // face left
}
if (rightPressed)
{
rutte.x += 5;
rutte.scaleX = -1; // face right
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
Thank you in advance for all the help you can give me.
Kind Regards.
in flash there is a fundamental aspect of how timeline and scenes works. Once you move away from a frame to another frame, The content of the frame and it's properties/states/actions are gone and reseted.
Personally I recommend you use a single scene with a timeline divided into frames labeled, there you can specify a layer for the actions and global variables, one for the user interface and another one for the specific actions of each frame. example:
So you never lose the reference of variables because the frame is not constantly recreated, all your important actions, including you timer, should be in your first frame.
I was testing, here you can see a working example: http://db.tt/FZuQVvt3. Here you can download the FLA file to be used as a base for your project: http://db.tt/RHG9G5lo