Custom button with image - actionscript-3

Hi i'm trying to create a button in actionscript 3 that will be made by a custom image ( a car.jpg from my HDD ). Is it possible to do that with SimpleButton class ( i mean is there a way to load the image and attach it to the button ?) or is there another way?
If i would like to have animation also in the button when let's say i roll over the mouse pointer from the button what do i have to do?
ps: i would like to use only actionscript 3 ( i mean all the project is done as a .as file that is linked to Document class ).

Although George Profenza already pointed the best solution under the comment if feel curious how you could implement the SimpleButton class you might want to take a look into th as3 document reference examples:
adobe livedocs - SimpleButton Example
I have also wrote a simple example that only makes a "simple button" in a "complicated button" but makes use of SimpleButton class, so you can take a look how you can extend the class, and give to each state it's own graphic. Here is the code:
// this goes in your app
var button:MySimpleButton = new MySimpleButton();
addChild(button);
MySimpleButton.as
package
{
import flash.display.DisplayObject;
import flash.display.SimpleButton;
import flash.display.Sprite;
public class MySimpleButton extends SimpleButton
{
private var upAlpha : Number = 1;
private var overAlpha : Number = 0.5;
public function MySimpleButton(upState : DisplayObject = null, overState : DisplayObject = null, downState : DisplayObject = null, hitTestState : DisplayObject = null)
{
upState = new ButtonImgDisplayState( upAlpha);
overState = new ButtonImgDisplayState( overAlpha);
downState = new ButtonImgDisplayState( upAlpha);
hitTestState = new ButtonImgDisplayState( upAlpha);
super(upState, overState, downState, hitTestState);
}
}
}
ButtonImgDisplayState.as
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.URLRequest;
public class ButtonImgDisplayState extends Sprite
{
public function ButtonImgDisplayState(_alpha:Number)
{
var my_loader : Loader = new Loader();
my_loader.load(new URLRequest("car.jpg"));
addChild(my_loader);
this.alpha = _alpha;
}
}
}
The thing about the SimpleButton is to spare you to set listeners, but you are obligated to go around the states that are separated DisplayObjects making you button a more rigid thing when dealing with transitions between states.
Hope you find this useful.

Related

Add your Stage in a ScrollWindow ActionScript 3.0

I'm gonna try explaining my situation with images, to make sure that everyone will understand what i want to succeed.
First of all i have 3 files:
GenImage.fla which is linked to class GeneralImage.as
and it only contains the following picture (I tried to make the image Movie Clip but again it's not working):
Pic1
and a file named ScrollUI.as which contains the class ScrollUI.
What i want to succeed is from my GeneralImage class to create a ScrollUi item, pass the stage, and there create a ScrollPane which makes the picture look like this:
Pic2
The center part of the second image is the ScrollPane Component, which i need to make it Scrollable through the whole image. I managed to get it in my screen but i can't put the Stage in it and make it scrollable.
These are my codes :
GeneralImage.as
package {
import flash.display.*;
import flash.events.*;
import flash.display.Stage;
import ScrollUI;
public class GeneralImage extends MovieClip
{
public function GeneralImage()
{
var k = new ScrollUI();
k.ScrollConstructor(this);
}
}
}
ScrollUI.as
package
{
import flash.display.*;
import flash.events.*;
import fl.containers.ScrollPane;
import fl.events.ScrollEvent;
import fl.controls.ScrollPolicy;
public class ScrollUI extends MovieClip
{
private var _mainStage:Stage;
var aBox:MovieClip = new MovieClip();
var aSp:ScrollPane = new ScrollPane();
public function ScrollUI()
{
}
function ScrollConstructor(stage:Object):void
{
_mainStage = (stage as MovieClip).stage;
aBox == stage as MovieClip;
aSp.source == aBox ;
_mainStage.addChild(aBox);
aSp.setSize(300,300);
aSp.move(150, 75);
aSp.scrollDrag = true;
aSp.horizontalScrollPolicy=ScrollPolicy.OFF;
_mainStage.addChild(aSp);
}
}
}
So what i want it to set the Source of the Scroll Pane ( which is named aSp ) to be the _mainStage which is the stage i get from GeneralImage
Your issues is likely these two lines:
aBox == stage as MovieClip;
aSp.source == aBox ;
You're doing a comparison by using two ==, which effectively does nothing in your case. Use a single = to assign a value.
This is how I would suggest you approach this:
In the FlashPro library, find your image asset (I'm going to assume you have it wrapped in a MovieClip), right-click (or command click) and go to it's properties. Check the "export for actionscript" box, and give it a meaningful class name (for my example, I'll assume you called it MyImage)
Then you could do the following:
ScrollUI Class
package
{
import flash.display.Sprite;
import fl.containers.ScrollPane;
import fl.events.ScrollEvent;
import fl.controls.ScrollPolicy;
public class ScrollUI extends Sprite
{
//just declare the variables here, don't assign them new values
private var aBox:MovieClip;
private var aSp:ScrollPane;
public function ScrollUI()
{
//use the actual constructor...
aSp = new ScrollPane(); //instantiate a new scroll pane
addChild(aSp); //add the scroll pane to the display
aBox = new MyImage(); //instantiate an new MyImage from the library
//set the scroll pane properties
aSp.source = aBox; //you had two = signs here before, which doesn't actually assign anything it compares
aSp.setSize(300,300);
aSp.move(150, 75);
aSp.scrollDrag = true;
aSp.horizontalScrollPolicy=ScrollPolicy.OFF;
}
}
}
Document Class
package {
import ScrollUI;
public class GeneralImage extends MovieClip
{
public function GeneralImage()
{
var k:ScrollUI = new ScrollUI(); //create a new instance of the ScrollUI class
addChild(k); //add the scrollUI object to the display;
//OR, you just do this:
//addChild(new ScrollUI());
}
}
}
You could also just set the .source property of the scroll pane to the physical path of your image.
I found the solution, Thanks Batman for his help, I changes some things into my code and the program is working.
First of all as Batman said , In my GenImage.fla i made the logo a MovieClip and i named it "wholemap"
Here are my codes :
GeneralImage.as
package {
import flash.display.*;
import flash.events.*;
import flash.display.Stage;
import ScrollUI;
public class GeneralImage extends MovieClip
{
//as Batman indicated, I should have used the ScrollUI constructor, but
//except for the Stage, i also send the wholemap that is in my GenImage.fla
//<< this.getChildByName("wholemap") as MovieClip) >>
public function GeneralImage()
{
var k = new ScrollUI(this, this.getChildByName("wholemap") as MovieClip);
}
}
}
ScrollUI.as
package
{
import flash.display.*;
import flash.events.*;
import fl.containers.ScrollPane;
import fl.events.ScrollEvent;
import fl.controls.ScrollPolicy;
public class ScrollUI extends MovieClip
{
private var _mainStage:Stage;
var aBox:MovieClip = new MovieClip();
//So our constructor gets 2 items, a Stage, and a MovieClip
public function ScrollUI(stage:Object, pic:MovieClip)
{
//We set the Stage at the variable _mainStage with that way:
_mainStage = (stage as MovieClip).stage;
//We set the Image that we will take at our clip variable :
var clip:MovieClip = pic;
//And we send the Movieclip (clip) in our ScrollConstructor function
ScrollConstructor(clip);
}
function ScrollConstructor(Clip:MovieClip):void
{
var aSp:ScrollPane = new ScrollPane();
aBox = Clip;
_mainStage.addChild(aBox);
aSp.source = aBox ;
aSp.setSize(300,300);
aSp.move(150, 75);
aSp.scrollDrag = true;
aSp.horizontalScrollPolicy=ScrollPolicy.OFF;
aSp.verticalScrollPolicy=ScrollPolicy.OFF;
_mainStage.addChild(aSp);
}
}
}
Thank you very much for your assistance, I hope if someone else come across with this problem to be able to solve it with this Answer

Starling library - When I try and change cursor to image...it doesn't work (and a sprite element dissapears from stage)

I am using Starling to make a really, really, really easy game - I am just trying to add a stationary sprite to the stage...and make it so that when the mouse touches the sprite...the game "stops" and a score is sent. I haven't tried implementing hitTest yet for the collision, but I have run into a sort of conflict problem where, when I comment out the line(s) that is supposed to change the cursor image (see Startup.as - stage.addEventListener(TouchEvent.TOUCH, touchHandler); and createCustomeCursor), the instance of AvatarEnemy (see enemy in Game.as) does what it should, and is placed in the center of the screen. When I comment in the line that is supposed to change the cursor: a). the cursor doesn't change, and b.) the enemy sprite disappears. When I comment out the same lines - the enemy sprite reappears (but obviously, the cursor doesn't work - not that it was working in the first place). Why is this happening? My code is below - someone mentioned something about not doing things before Starling is initialized here (a question I asked, which is a precursor to this one) - but I'm not sure what they meant because it at least seems like all my code is in the right place.
Game.as
package
{
import Classes.AvatarEnemy;
import starling.display.Sprite;
public class Game extends Sprite
{
//private var juggler:Juggler = Starling.juggler;
private var enemy:AvatarEnemy;
public function Game()
{
enemy = new AvatarEnemy();
addChild(enemy);
}
}
}
Startup.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.geom.Point;
import starling.core.Starling;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.display.DisplayObject;
import flash.display.BitmapData;
import flash.ui.MouseCursorData;
import flash.ui.Mouse;
[SWF(width="500", height="500", frameRate="30", backgroundColor="#FFFFFF")]
public class Startup extends Sprite
{
private var mStarling:Starling;
[Embed(source="Classes/Avatarpic.png")]
private const Cursor:Class;
public var cursor:DisplayObject;
public function Startup()
{
// Create a Starling instance that will run the "Game" class
mStarling = new Starling(Game, stage);
mStarling.start();
// These settings are recommended to avoid problems with touch handling
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
createCustomCursor();
stage.addEventListener(TouchEvent.TOUCH, touchHandler);
}
private function touchHandler(event:TouchEvent):void
{
var touch:Touch = event.getTouch(cursor);
if(touch.phase){
{
var localPos:Point = touch.getLocation(cursor);
trace("Touched object at position: " + localPos);
}
}
}
public function createCustomCursor():void
{
var cursorBitmaps:Vector.<BitmapData> = new Vector.<BitmapData>();
cursorBitmaps.push((new Cursor() as Bitmap).bitmapData);
var mouseCursorData:MouseCursorData = new MouseCursorData();
mouseCursorData.data = cursorBitmaps;
mouseCursorData.frameRate = 30;
mouseCursorData.hotSpot = new Point(0, 0);
Mouse.registerCursor("customCursor", mouseCursorData);
Mouse.cursor = "customCursor";
}
}
}
Any help would be greatly appreciated (if you need a copy of the code (it's an Adobe Flash Builder 4.7 project - I have made a git repo - just comment if you want the link).
UPDATE
I realized I wasn't registering the image as the cursor - I updated my Startup.as file to reflect changes (take a look at createCustomCursor function) - the cursor still isn't working, and the sprite that is supposed to appear is still not appearing.
Also - just in case you want to see where AvatarEnemy is coming from:
AvatarEnemy.as
package Classes
{
import starling.display.Image;
import starling.display.Sprite;
import starling.textures.Texture;
public class AvatarEnemy extends Sprite
{
[Embed(source='Enemypic.png')]
private static var Enemypic:Class;
private var texture:Texture = Texture.fromBitmap(new Enemypic());
private var image:Image = new Image(texture);
public function AvatarEnemy()
{
image.x = 0;
image.y = 200;
addChild(image);
}
}
}
UPDATE
I resolved why the sprite was disappearing - I needed to put the starling initialize code (mStarling.start() and mStarling = new Starling(Game, stage);) above all the stage.something lines. I edited code to reflect what I did in Startup.as. I still need help with the cursor though.
I have been practicing with your code and it all seems fine, but ive been using a different image, so that must be the problem.
Make sure the size of the image is below 32x32, thats the max size of an cursor image, otherwise the OS won't accept it.

AS3 - How to get current scene name in class

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;
}
}
}
}

AS3 How to get custom class instances to access variables on timeline?

Please help me understand how to get custom class instances to access variables defined on the timeline.
I am not using a document class, because this program will be a memory experiment with multiple stages, so using the timeline works best for me. (FWIW, I'm also not using xml; just trying to keep this as simple as possible.) For now I've just made a simple one-frame fla on which I'm trying to make a vocabulary test. I have made a custom class called VocabQ, which in turn contains 4 instances of a custom class called VocabButton. Right now, clicking one of those buttons just traces the button label. But I want it to also update the value of the String variable CurrentResponse, which is declared on the timeline. If I just try to reference CurrentResponse from the VocabButton class, I get an error "1120: Access of undefined property...".
I've tried a variety of approaches based on discussions I've found across the internet, but have not yet had success and am only getting more confused. Please help! (Simple solutions would be greatly appreciated, if such exist!) See code below.
thank you, ~jason
code in timeline:
import VocabQ;
import flash.display.*;
stop();
var CurrentResponse:String="NA";
var VocabQuestion = new VocabQ("VocabWord",["answerA","answerB","answerC","answerD"]);
addChild(VocabQuestion);
VocabQ.as code:
package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.*;
import VocabButton;
public class VocabQ extends MovieClip{
private var _VocabWordText:String;
private var _VocabWord:TextField;
private var _ResponseOptions:Array;
public function VocabQ(VocabWordText:String,ResponseOptions:Array){
_VocabWordText=VocabWordText;
_ResponseOptions=ResponseOptions;
build();
}
private function build():void{
_VocabWord = new TextField();
_VocabWord.text=_VocabWordText;
_VocabWord.x=25;
_VocabWord.y=25;
_VocabWord.textColor = 0x000000;
addChild(_VocabWord);
for (var i:int; i < _ResponseOptions.length; i++){
var _VocabButton:VocabButton = new VocabButton(_ResponseOptions[i]);
_VocabButton.x = 25 + (_VocabWord.width) + 10 + ((_VocabButton.width + 2) * i);
_VocabButton.y = 25;
addChild(_VocabButton);
}
}
}
}
VocabButton.as code:
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class VocabButton extends MovieClip{
private var _btnLabel:TextField;
public function VocabButton(labl:String){
_btnLabel = new TextField();
_btnLabel.textColor = 0x000000;
_btnLabel.text = labl;
_btnLabel.border=true;
_btnLabel.borderColor=0x000000;
_btnLabel.background=true;
_btnLabel.backgroundColor= 0xDAF4F0;
_btnLabel.mouseEnabled = false;
_btnLabel.selectable=false;
_btnLabel.width=100;
_btnLabel.height=18;
buttonMode=true;
useHandCursor=true;
addEventListener(MouseEvent.CLICK,onClick,false,0,true);
addChild(_btnLabel);
}
private function onClick(evt:MouseEvent):void{
trace(_btnLabel.text);
//CurrentResponse=_btnLabel.text; //causes error 1120: Access of undefined property...
}
}
}
This should work:
private function onClick(evt:MouseEvent):void {
trace(_btnLabel.text);
evt.currentTarget.root.CurrentResponse=_btnLabel.text;
}
See the discussion here: http://www.actionscript.org/forums/showthread.php3?t=161188
Personally, rather than trying to access the timeline like this, I would prefer to add a click listener
to your VocabQuestion on the timeline and allow the Event to Bubble Up, or, if you are going to have a number of buttons within your class, to extend EventDispatcher and create some custom events which can likewise be handled on your timeline, but for a simple test, accessing the root property should be good enough.

need help - actionscript 3 simple button class

I have very simple class, but it doesn't work? What could be wrong with this?
package {
import flash.display.Sprite;
import fl.controls.Button;
public class t_class extends Sprite {
private var b:Button;
public function t_class():void{
b = new Button();
b.width = 150;
b.label = "button label";
b.move(10, 150);
b.enabled = false;
addChild(b);
}
}
}
Assuming that you are adding an instance of your button class as a child to the stage as others have mentioned above:
package
{
//Imports
import flash.display.Sprite;
import com.wherever.is.t_class;
//Class
public class DocumentClass extends Sprite
{
//Constructor
public function DocumentClass()
{
var myButton:t_class = new t_class();
addChild(myButton);
}
}
}
The good news is that your t_class code is correct (minus a few deviations from AS3 code conventions, but i digress). However, in order to use Flash Components they must be physically imported into the library in addition to being imported with code. If they are not present in the library then Flash doesn't know they exist and you will receive the following during compile:
"ERROR 1046: Type was not found or was not a compile-time constant: Button."
Goto Window > Components, select User Interface > Button and drag the component into your library. Rebuild and you should see your button.