AS3 Undefined Method for button class - actionscript-3

I'm trying to make a button link to an external page. In the first frame I placed this code:
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
var request:URLRequest = new URLRequest("http://pruebainteligente.com");
request.method = URLRequestMethod.GET;
var target:String = "_blank";
navigateToURL(request, target);
}
To make the button class I went to the library > properties > And named the button Class "myButton".
What I'm doing wrong?

myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
If that code is on the frame itself it is referring to an instance of an object/symbol, not the class itself. You will need to drag your button from the library and place it onto the stage, set its instance name (in the properties tab) to "myButton", then your code will work.
Alternatively you can add a copy of your object purely through code:
var newButton = new myButton();
addChild(newButton);
newButton.addEventListener(MouseEvent.CLICK, onMouseClick);

Related

How can I call a function in my as file from my fla without linking it?

I've got an .as file in the same folder of my FLA file.
I'd like to call trigger a function from an action frame of my as file.
My as file is like that :
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.MouseEvent;
public class Game extends MovieClip {
var words:Array = new Array; //a mini database to hold the words
var rand1:int;var rand2:int; //variables used for randomization
var scramble_Array:Array = new Array; //array used to scramble the word
var unscramble_Array:Array = new Array; //array to hold the unscrambled word
var temp:String; //temporary variable
var pauser:Boolean; //used to pause the game
public function Game() {
getword();
checker=new button_chk;addChild(checker);
checker.x=150;checker.y=400;
checker.addEventListener(MouseEvent.CLICK, check_answer);
}
...
...
How can I call the function "Game" in my FLA file using action frame ?
I've tried to do
Game();
But it says that one argument is missing.
It is a class constructor, you don't just call it, you instantiate (create a new instance of) the class with the new operator:
import Game;
var aGame:Game = new Game;

How to set JPG type of image screenshot saved from flash using Actionscript 3?

I have a problem when i wanna save screenshot entire flash stage using ActionScript 3. when i click button that use function SimpanGbr it's works for saving image with name "NamaGambar" but without type of JPG image. How to save image with it's type. I use these codes:
stop();
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import com.adobe.images.JPGEncoder;
stage.displayState = StageDisplayState.FULL_SCREEN;
exit.addEventListener(MouseEvent.CLICK,keluar);
function keluar(event:MouseEvent):void
{
fscommand("quit");
}
function SimpanGbr(e:MouseEvent):void {
var qImageData:BitmapData = new BitmapData(800, 600);
qImageData.draw(stage);
var qEncoder:JPGEncoder = new JPGEncoder(100);
var qBytes:ByteArray = qEncoder.encode(qImageData);
var qFile:FileReference = new FileReference();
var nama:String="NamaGambar";
qFile.save(qBytes, nama+".jpg");
}
You can do so using this library.
After downloading copy the source files (the com folder) on to your Class path or your application root directory. Apart from the JPEGEncoder, this library is packaged with a lot of other classes related to Hasing, Text, Date etc.
This is how we save an image from a flash movie to a local machine.
1) Create a BitmapData object out of the MovieClip or the Loader.
2) Encode the BitmapData using the JPEGEncoder and form a ByteArray.
3) Use FileReference.save() to download the image on to the User machine.
See an example:
Create a blank Flash movie and name it as “ImageSave.fla”. In the same folder create “ImageSave.as” file and copy the below code in to it. You need the “com” folder copied from the as3corelib to this folder. I have a button on the stage named “save_mc” which triggers the saveImage function.
package {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.utils.ByteArray;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import com.adobe.images.JPGEncoder;
public class ImageSave extends MovieClip {
private var imgFile:URLRequest;
private var img:MovieClip;
private var imgLoader:Loader;
private var file:FileReference;
public function ImageSave() {
imgFile = new URLRequest("coffee.jpg"); //change it to your image path
imgLoader = new Loader();
imgLoader.load(imgFile);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
save_mc.addEventListener(MouseEvent.CLICK, saveImage);
}
private function onLoaded(evt:Event):void {
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
imgLoader.cacheAsBitmap = true;
addChild(imgLoader);
}
function saveImage(e:MouseEvent):void {
var myBitmapData:BitmapData = new BitmapData(imgLoader.width, imgLoader.height);
myBitmapData.draw(imgLoader);
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var imgByteData:ByteArray = jpgEncoder.encode(myBitmapData);
file = new FileReference();
file.browse(new Array(new FileFilter("Images (*.jpg, *.jpeg)", "*.jpg;*.jpeg")));
file.save(imgByteData, "test.jpg");
}
}
}
For your convenience you can download a working copy of this sample here.

Flash Actionscript GetURL not working

i´m making a banner for a webpage in flash. And i want to put buttons with links there.
So far i got it working with this script:
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
fisicleta.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
var request:URLRequest = newURLRequest("url");
request.method = URLRequestMethod.GET;
var target:String = "_blank";
navigateToURL(request, target);
}
But his opens the link in a new tab, and i´m trying to keep it all in the same tab. I tried changing the target to "_self" but the link goes dead.
Thanks in advance!
You can use:
navigateToURL(new URLRequest("url"), "_top");
it will call your url from the window.top location and not from the current self location.

ActionScript3: Changing button text

I have a button instance named Button that I have in my movie, this instance has a Dynamic Text object in it named myText, how can I change the text? I already tried Button.myText.text = "Stuff";
I get the error "Scene 1, Layer 'Layer 1', Frame 1, Line 7 1119: Access of possibly undefined property myText through a reference with static type flash.display:SimpleButton." When I compile.
AS:
import flash.events.MouseEvent;
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void{
TheButton.myText.text = "Moo";
}
You can't use the dot syntax to access a display object container's child display objects in AS3 as you did in AS2. Normally you would use the display object container's getChildByName() method to get its child display objects, but because your dealing with an instance of SimpleButton which is a subclass of DisplayObject that method doesn't exist. The simple solution is to change you button from a button to a movieclip after which the following should work:
TheButton.addEventListener(MouseEvent.CLICK, onClick);
function onClick(Event:MouseEvent):void
{
TheButton.getChildByName("myText").text = "Moo";
}
Note: the TextField display object in the TheButton display object container must have an instance name of "myText" and obviously the TheButton display object container must have an instance name of "TheButton".
Also if your going with this approach you may want to rewrite the code as follows:
import flash.display.DisplayObjectContainer
import flash.events.MouseEvent;
import flash.text.TextField;
button.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(e:MouseEvent):void
{
var button:DisplayObjectContainer = DisplayObjectContainer(e.target);
var textField:TextField = TextField(button.getChildByName("textField"));
textField.text = "Moo";
}// end function
[UPDATE]
Another solution is to create a movieclip/sprite object for the SimpleButton object's upState, overState and downState properties like the following:
import flash.display.DisplayObjectContainer;
import flash.display.SimpleButton;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
var simpleButton:SimpleButton = new SimpleButton();
addChild(simpleButton);
var content:Sprite = new Sprite();
draw(content.graphics, 0xFF0000);
var textField:TextField = new TextField();
textField.name = "textField";
textField.text = "UP";
content.addChild(textField);
simpleButton.upState = content;
simpleButton.overState = content;
simpleButton.downState = content;
simpleButton.hitTestState = content;
simpleButton.addEventListener(MouseEvent.MOUSE_OVER, onSimpleButtonMouseOver);
simpleButton.addEventListener(MouseEvent.MOUSE_DOWN, onSimpleButtonMouseDown);
simpleButton.addEventListener(MouseEvent.MOUSE_OUT, onSimpleButtonMouseOut);
simpleButton.addEventListener(MouseEvent.MOUSE_UP, onSimpleButtonMouseUp);
function onSimpleButtonMouseOver(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).overState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "OVER";
draw(content.graphics, 0xC8C8C8);
}// end function
function onSimpleButtonMouseDown(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).downState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "DOWN";
draw(content.graphics, 0x646464);
}// end function
function onSimpleButtonMouseUp(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).overState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "OVER";
draw(content.graphics, 0xC8C8C8);
}// end function
function onSimpleButtonMouseOut(e:MouseEvent):void
{
var content:DisplayObjectContainer = DisplayObjectContainer(SimpleButton(e.target).upState);
var textField:TextField = TextField(content.getChildByName("textField"));
textField.text = "UP";
draw(content.graphics, 0xFF0000);
}// end function
function draw(graphics:Graphics, color:uint):void
{
graphics.clear();
graphics.beginFill(color)
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
}// end function
It's extremely easy.
Suppose I have a button that was created from the Components menu called myBtn.
myBtn.label = "Awesome text";
You use label instead of creating a textfield over it then setting the texfield's text.
That's it!
In the absence of extra information as to whether you're getting errors and what they are, my first assumption would be that the system is having trouble with the variable name "Button". There is already a class named "Button", so I would think the system is thinking you are trying to call a class-level method/var of class Button instead of accessing a variable named "Button". I would try changing the name of the button var to something unique.
Children of SimpleButton are inaccessible from Actionscript; notice that it does not actually extend DisplayObjectContainer. The SimpleButton class is a bit of a mutant.
You will have to use a container MovieClip and put the SimpleButton and the label TextField inside. Alternatively you can roll your own button out of a MovieClip or use a component library. SimpleButton is not meant for this kind of use.

ActionScript Retrieving Width/Height Of Loader Image

i'm trying to access the width and height of an image that is added to the stage via a custom LoadImage class. the trace results are 0 even though the image displays correctly. what is the problem?
//frame script
var image:LoadImage = new LoadImage("myImage.jpeg");
addChild(image);
trace(image.width); //returns 0
//-------------------------
package
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
public class LoadImage extends Sprite
{
public function LoadImage(imageURL:String)
{
//Load Image
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageHandler);
imageLoader.load(new URLRequest(imageURL));
}
private function imageHandler(evt:Event):void
{
addChild(evt.target.content);
}
}
}
If you're trying to access it right after you instantiate it, you don't have access to it's properties. You will have to make it event driven:
class LoadImage loads image
frame script listens for a complete event from LoadImage
LoadImage loads the image, once it has it's hands on it it dispatches the event
frame script works with the data
you need to make an event in LoadImage and once done in imageHandler, dispatch that. When you make your new LoadImage, set up the listener
var image:LoadImage = new LoadImage("myImage.jpeg");
image.addEventListener("complete", loadedImage); //or whatever you call the event
function loadedImage(evt:Event) {
addChild( evt.target );
trace(evt.target.content.width); //returns 0
}
you're trying to get the width and height before the image is completely loaded? see this question for more detail.