i want to Load image from hard disk and display in my flash movie i have little code when i click on button it open hyperlink so anybody know about image loading function
package{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
public class Main extends Sprite{
public function Main():void{
z_cst.addEventListener(MouseEvent.CLICK,buttonClick);
z_kp.addEventListener(MouseEvent.CLICK,buttonClick);
z_kr.addEventListener(MouseEvent.CLICK,buttonClick);
z_ka.addEventListener(MouseEvent.CLICK,buttonClick);
z_rs.addEventListener(MouseEvent.CLICK,buttonClick);
z_c.addEventListener(MouseEvent.CLICK,buttonClick);
z_vp.addEventListener(MouseEvent.CLICK,buttonClick);
z_cr.addEventListener(MouseEvent.CLICK,buttonClick);
z_cs.addEventListener(MouseEvent.CLICK,buttonClick);
}
private function buttonClick(e:MouseEvent):void{
var url:URLRequest=new URLRequest();
switch(e.target.name){
case "z_cst": url.url="http://www.star.com"; break;
case "z_kp": url.url="http://www.star.com"; break;
case "z_kr": url.url="http://www.star.com"; break;
case "z_ka": url.url="http://www.star.com"; break;
case "z_rs": url.url="http://www.gear.com"; break;
case "z_c": url.url="http://www.apple.com"; break;
case "z_vp": url.url="http://www.images.com"; break;
case "z_cr": url.url="http://www.buy.com"; break;
case "z_cs": url.url="http://www.contact.com"; break;
}
navigateToURL(url,"_blank");
}
}
}
Look up the Loader{} class for loading images - http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html
Added some code - (wish code in comments could be formatted as code)
From your comment, it looks like you would have gotten it. What you were missing was that you needed to call the Loader object's load() method and pass in the URLRequest object to it. Here is some code that does this loosely following your previous code:
private function buttonClick(e:MouseEvent):void {
var loader:Loader = new Loader();
this.addChild(loader);
switch(e.target.name) {
case "z_cst":
loader.load(new URLRequest("/data/1.jpg"));
break;
case "z_kp":
loader.load(new URLRequest("/data/2.jpg"));
break;
case "z_kr":
loader.load(new URLRequest("/data/3.jpg"));
break;
default:
trace("buttonClick() - No matching name found");
this.removeChild(loader); // remove the Loader object from the stage as it is 'empty'
break;
}
}
I added the 'default' block to the switch block to help check for possible errors. A Loader object is a display object, so you can position it just like any other display object.
Related
I'm trying to setup a class that handles screen navigation for a game; called MenuNavigation. I have extended this class with MovieClip so I should be able to access gotoAndStop functions, but I keep getting the error -
"1180 - Access of undefined method gotoAndStop"
. I have also tried:
MovieClip(root).gotoAndStop(frameLabel)
MovieClip(root).gotoAndStop(frameLabel)
Stage.gotoAndStop(frameLabel)"
these do not work either. I have made a few other classes that have extended MovieClip would be this be an issue? is there a limit? Its weird I have done other flash projects and done the exact same thing and never had an issue. I'm sure its something stupid on by behalf. Thanks
Here is the code:
package
{
import flash.display.*;
import flash.events.*;
public class MenuNavigation extends MovieClip
{
public function MenuNavigation()
{
// constructor code
}
// function deals with button navigation handling between frames
public static function loadScreen(evt:MouseEvent)
{
switch(evt.target.name)
{
case "playButton":
gotoAndStop("aboutGame");
break;
case "creditsButton":
gotoAndStop("credits");
break;
case "aboutNextButton":
gotoAndStop("instructions");
break;
case "aboutBackButton":
gotoAndStop("mainMenu");
break;
case "instructionsBackButton":
gotoAndStop("aboutGame");
break;
case "instructionsPlayButton":
gotoAndStop("game");
break;
case "creditsBackButton":
gotoAndStop("mainMenu");
break;
}
}
}
}
public function loadScreen(evt:MouseEvent)
{
....
}
Your main problem is with the scopes. The function loadScreen shouldn't be static. If its static the scope of the function is different and won't have the MovieClip object props and functions.
I'm playing around with flash, and I've created multiple scenes for things like menu's, buttons, etc. When trying to add event handlers for buttons that are in one scene, but not others, the compiler complains saying that it can't reference to objects that don't exist.
I figured the solution to be simple... Get the scene name, match that against an if statement and load the event handlers through the if statements...
However, after digging around on the net for far too long, I just can't seem to find a way to do this properly. Does anyone know a way?
I've tried using the following :
var scene:Scene = myflvandclassname.currentScene;
var sName:String = MovieClip.currentScene.name;
Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class".
Omit MovieClip and scenes as source for organising your project, and code on the timeline. Use Document class as entry point. This tutorial should help you to grasp main concept.
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
private function setup():void {
const padding:int = 20;
//Initiate your UI components and place them in the display list
var menu:MyMenu = new MyMenu();
var buttons:MyFooterButtons = new MyFooterButtons();
var etc:AnotherComponent = new AnotherComponent();
addChild(menu);
addChild(buttons);
addChild(etc);
menu.x = menu.y = padding;
//Place them and initialise with concrete information
}
}
}
For example, MyMenu, MyFooterButtons, AnotherComponent could be MovieClip/Sprite in the library with export settings, where you did all your work with placement, styling, etc
I had a same problem. I wanted to check from an external Class the current scene name and depending on the name (name of the game level) to pass some values in some attributes… So what I did and it worked is that.
//main in 1st frame of the fla
stop();
var myCheckSceneClass: CheckSceneClass = new CheckSceneClass();
myCheckSceneClass.myCurrentScene = currentScene;
myCheckSceneClass.checkScene();
//CheckSceneClass
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Scene;
public class CheckSceneClass extends flash.display.MovieClip {
public var myCurrentScene : Scene;
public function CheckSceneClass () {
}
public function checkScene() {
switch (myCurrentScene.name) {
case "Scene 1":
trace("It is the 1st Scene");
break;
default:
trace("Error with Scene Name");
break;
}
}
}
}
I have a "score1.as" class in which it will access a data from an swf and display it into
my "finalscore.fla"...I was able to pass and trace the data successfully into my "finalscore.fla"..But my problem is this: Though I was able to access the data by tracing it, I can't display it to my dynamic text...I thought by simply typing "txtScore.text = ("Score: " + lol1.go() );" would solve the problem but it didn't...Please help..Here's my code..bY the way, I'm using actionscript 3.0..
score1.as:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class score1 extends Sprite
{
private var loader:Loader;
public function Parent()
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("savescore.swf"));//This is the swf where in the data came from
}
public function onLoaded(e:Event):void
{
trace(loader.content['currentScore']);
}
public function go():int{
return loader.content['currentScore'];//This is the data being accessed
}
}
}
finalscore.fla:
var lol1:score1 = new score1();
txtScore.text = ("Score: " + lol1.go() ); // This is where I can't display the data
lol1.Parent();//I successfully traced the data
Check your embedded fonts. You are likely only embedding the characters from the font that were present at compile time. For example, if you have a textfield with "Hello World!", and you wanted to do textfield.text = "Jiggly Jello Lord!" would display as "l ello ord", because only those characters were present in the textfield during compile.
Edit:
I took a closer look at your code. You need to call Parent() before you can get your content. In fact, rename it to be the constructor, and you should be good.
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class score1 extends Sprite {
public var loader:Loader;
public function score1() {
// You need to run this code as your constructor.
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("savescore.swf"));
}
public function onLoaded(e:Event):void {
trace(loader.content['currentScore']);
}
public function go():int {
return loader.content['currentScore'];
}
}
}
You'd also want to wait for your content to be loaded before accessing the data. For this, you'd need to wait for your onLoaded to fire before trying txtScore.text = ("Score: " + lol1.go() );
Edit 2
onLoaded is how you know if it runs. It gets called when Event.COMPLETE fires. By the same extension, just register for that event from outside your class, or fire an event, or any other solution that completes after the data is loaded.
finalscore.fla:
var lol1:score1 = new score1();
lol1.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, scoreReady);
function scoreReady(e:Event):void {
txtScore.text = "Score:" + lol1.go();
}
I have a Library item: MovieClip with a single ComboBox component inside. The item has a Class assigned to it, myMC.
What I want to achieve is that a call from the main movie like var mmc:myMC = new myMC( stage); would:
initialize the ComboBox instance's values;
place the myMC instance on stage (or inside another MC).
So inside the myMC Constructor I wrote smth like:
public function myMc( theStage:flash.display.Stage) {
if( stage == null) this.addEventListener( Event.ADDED_TO_STAGE, init);
theStage.addChild( this);
}
public function init( e:Event = null) {
var Data:Array= new Array(
{'label1' : 'item1'},
{'label2' : 'item2'},
{'label3' : 'item3'}
);
cbox.dataProvider = new DataProvider( Data);
}
cbox is the name of ComboBox instance within myMC.
What happens is that the ComboBox doesn't have any values assigned. This is a simplified example of my problem, whereas the actual case involves more UI components — all of them miss their values.
Debugger show the component objects of correct type, with the values - but they miss from the stage objects shown!
Please explain, what am I doing wrong - and why does the instance on stage is somehow different from the AS instantiated Object?
Ok, seems that it was my mistake: wrong types and messy code.
Here's an example that works afterall. So, maybe its not the best practice to put an object onstage by its own constructor, however this works. The working example sources.
Main document has a class DocTest:
package {
import flash.display.Sprite;
import libItem;
public class DocTest extends Sprite {
public function DocTest() {
var instance:libItem = new libItem( this);
}
}
}
And the library item has a ComboBox and a text field inside, and the following Class code attached:
package {
import fl.data.DataProvider;
import flash.display.Sprite;
import flash.events.Event;
public class libItem extends Sprite{
public function libItem( theParent:flash.display.Sprite) {
if( stage == null) this.addEventListener( Event.ADDED_TO_STAGE, init);
else init();
theParent.addChild( this);
}
public function init( e:Event = null) {
var Data:Array= new Array(
{label:'label1', data: 'item1'},
{label:'label2', data: 'item2'},
{label:'label3', data: 'item3'}
);
cbox.dataProvider = new DataProvider( Data);
x = 200;
y = 100;
myText.text = 'Woo-hoo!';
}
}
}
Hope this helps someone. Sorry for the studid question.
[NOTE: Right off the bat, I already know the popular theory about "no code on MovieClips," so please don't reply with that (or
downvote because of it). Every project is different. I just need the answer.]
I have a project in Adobe Flash CS5.5, Adobe AIR 3, and ActionScript 3.
I need to call a function on the main timeline of the project that the document class is linked to from inside the document class. For the sake of example, let's call this function "Ring()".
How do I call this function, "Ring()" from inside my document class?
Put the function you want to call in your document class, and dispatch a custom event (or any event, if the code is readable) from the timeline of the object and listen for that event on your document class.
So the code breakdown would look like this:
On some frame of the timeline in your document (should work on any object):
var customEvent:Event = new Event(Event.COMPLETE);
this.dispatchEvent(customEvent);
In your document class:
public function DocumentClass()
{
// get the reference to the object
lolcats.objectThatDispatchesEvent.addEventListener(Event.COMPLETE, _customHandler);
}
protected function _customHandler(event:Event):void
{
// FUNCTION NAMES SHOULD START WITH LOWERCASE! ^_^
Ring();
}
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
Basically you register any string that defines your event, Event.COMPLETE evaluates to "complete", you can just register whatever you want, like:
var custEvent = new Event("anyCustomString");
this.dispatchEvent(custEvent);
// catch it with
addEventListener("anyCustomString", _handler);
Well, since you seem to be using some oldskool ninja techniques, I would suggest that you should keep it simple and straightforward.
Say you have some functions on the main timeline:
function Ring1():String
{
return "Ring1() called!";
}
var Ring2:Function = function () : String
{
return "Ring2() called!";
};
The scenario for a document class of the said timeline would be like this:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.getQualifiedClassName;
import flash.utils.describeType;
public class Test extends MovieClip
{
public function Test()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
private function onMouseDown(event:MouseEvent):void
{
trace(getQualifiedClassName(this)+".onMouseDown()");
try {
var ring1:Function = this["Ring1"] as Function;
var ring2:Function = this["Ring2"] as Function;
} catch (error:Error) {
// ignore
}
if (ring1 != null) {
trace("\t", "Ring1", "=", ring1);
trace("\t", ring1());
} else {
trace("\t", "Ring1() function not found in "+this+"!");
}
if (ring2 != null) {
trace("\t", "Ring2", "=", ring2);
trace("\t", ring2());
} else {
trace("\t", "Ring2() function not found in "+this+"!");
}
// for your interest:
var doc:XML = describeType(this);
var ring1Node:XML = doc.descendants("method").(#name == "Ring1")[0];
var ring2Node:XML = doc.descendants("variable").(#name == "Ring2")[0];
trace("declaration of Ring1:", ring1Node.toXMLString());
trace("declaration of Ring2:", ring2Node.toXMLString());
// so, you may probably make use of reflection
// unless you need to reference dynamic members on the main timeline
}
}
}
See comments in the code above.