AS3 - Autosize button label text - actionscript-3

Is there a way in flash professional Actionscript3 to set button label with autosize?
I have many button on stage with different size and different label text on one line.
I want to auto-adapt size of entire label text inside my (all) button.
Thanks

Try this :
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
public class CreateButton extends MovieClip
{
public var btnBG: MovieClip;
public var btn_text: TextField;
public function CreateButton( btnName :String )
{
btn_text = new TextField();
btn_text.text = ( btnName ) ? btnName : '';
btn_text.autoSize = TextFieldAutoSize.LEFT;
btnBG.width = btn_text.width;
}
}
}

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

Why am I Gettign this Error ?: TypeError: Error #1010: A term is undefined and has no properties. at Main()

I am trying to make a small fanmade Zelda game. I am currently working on the menus, and have a small intro video that leads to the logo and start button being faded in. For some reason however my button is not removing the startPage and adding the filePage. My buttons name and ActionScript link in my library are "StartButton" and it is located in my StartPage movieclip. The filePage is also named "FilePage" aswell as it's AS3 link.
This is my code,
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Main extends MovieClip
{
var startPage:StartPage;
var filePage:FilePage;
var introMusic:Sound = new IntroMusic;
var introMusicChannel:SoundChannel = new SoundChannel();
public function Main()
{
introMusicChannel = introMusic.play();
startPage = new StartPage;
filePage = new FilePage;
addChild(startPage);
startPage.x = 275;
startPage.y = -12.50;
startPage.width = 795.95;
startPage.height = 498.40;
/*ADD EVENT LISTENERS
***********************/
startPage.addEventListener(MouseEvent.CLICK, onStartPageClick);
startPage.StartButton.addEventListener(MouseEvent.CLICK,
onStartButtonClick);
}
/*EVENT HANDLERS
***********************/
function onStartPageClick(event:MouseEvent):void
{
startPage.gotoAndPlay(599);
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(filePage);
filePage.x = 275.00;
filePage.y = 200.00;
filePage.width = 599.90;
filePage.height = 399.90;
removeChild(startPage);
introMusicChannel.stop();
}
}
}
I believe it is this line.
startPage.StartButton.addEventListener(MouseEvent.CLICK, onStartButtonClick);
Goto StartPage MovieClip, select the StartButton and look into properties panel. Make sure your StartButton has "instance name" as StartButton in properties panel

AS3 to get children in Gestureworks TouchSprite working

Recently just trying around with the Gestureworks multitouch.
Here I have some problem about button inside a TouchSprite, below is my code:
package
{
import com.gestureworks.core.GestureWorks;
import com.gestureworks.core.TouchSprite;
import com.gestureworks.events.GWGestureEvent;
import com.gestureworks.events.GWTouchEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Stage;
import flash.events.Event;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
public class Main extends GestureWorks
{
private var myDisplay:TouchSprite;
public function Main():void
{
super();
key = "xxx";
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
}
override protected function gestureworksInit():void
{
myDisplay = new TouchSprite();
myDisplay.gestureReleaseInertia = true;
myDisplay.gestureEvents = true;
myDisplay.disableNativeTransform = false;
myDisplay.disableAffineTransform = false;
myDisplay.mouseChildren = true;
myDisplay.gestureList = {"n-drag":true,"n-scale":true};
myDisplay.addChild(boliviaMap);
myDisplay.addChild(mapPink);
addChild(myDisplay);
myDisplay.addEventListener(GWGestureEvent.DRAG, gestureDragHandler);
myDisplay.addEventListener(GWGestureEvent.SWIPE, gestureSwipeHandler);
mapPink.addEventListener(TouchEvent.TOUCH_TAP, tapPink);
}
private function tapPink(e:TouchEvent):void
{
trace("PINK");
indicatorTxt.text = "PINK";
}
private function gestureDragHandler(event:GWGestureEvent):void
{
trace("g drag: ", event.value.dx, event.value.dy);
event.target.$x += event.value.dx;
event.target.$y += event.value.dy;
indicatorTxt.text = "g drag: " + event.value.dx + " " + event.value.dy;
}
}
}
apparently the myDisplay.mouseChildren = true; will make myDisplay not able to drag or scale anymore.
Before adding your bigMap button set the mouseChildren property of myDisplay to true:
myDisplay.mouseChildren = true;
This should pass mouseEvents to objects inside.
Also, depending on the effect you want (I'm assuming this is for a multi-touch screen of somesort), I'd suggest you should probably use a TouchEvent.TOUCH_TAP on bigMap instead of MouseEvent.CLICK(MouseEvents arent multi-touch and won't be responsive if there is some kind of touch on another part of the screen at the same time).
Edit:
Assuming boliviaMap and mapPink aren't TouchMovieClips or TouchSprites then do something like this:
myDisplay.graphics.beginFill(0x555555, 1);
myDisplay.graphics.drawRect(0,0,500,500);
myDisplay.graphics.endFill();
Then add your child assuming boliviaMap and mapPink are less than 500x500px:
myDisplay.addChild(boliviaMap);
myDisplay.addChild(mapPink);
Edit 2- A working example(for me anyways) combining TouchEvents with GWEvents:
package {
import flash.display.MovieClip;
import com.gestureworks.core.GestureWorks;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import com.gestureworks.core.TouchSprite;
import flash.display.Sprite;
import flash.events.TouchEvent;
public class Main extends GestureWorks {
public function Main() {
super();
key = "INSERT YOUR GW KEY HERE";
Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;
}
override protected function gestureworksInit():void
{
trace("GestureWorks has initialized");
var myTouchSprite:TouchSprite = new TouchSprite();
myTouchSprite.graphics.beginFill(0x555555,1);
myTouchSprite.graphics.drawRect(0,0,100,100);
myTouchSprite.graphics.endFill();
myTouchSprite.gestureReleaseInertia = false;
myTouchSprite.gestureEvents = true;
myTouchSprite.disableNativeTransform = false;
myTouchSprite.disableAffineTransform = false;
myTouchSprite.gestureList = {"n-drag":true, "n-rotate":true};
myTouchSprite.mouseChildren = true;
var myTappableSprite:Sprite = new Sprite();
myTappableSprite.graphics.beginFill(0xAD3059,1);
myTappableSprite.graphics.drawRect(30,30,40,40);
myTappableSprite.graphics.endFill();
myTappableSprite.addEventListener(TouchEvent.TOUCH_TAP, tappedThis);
//myTouchSprite.addChild(myTappableSprite);
myTouchSprite.addChild(myTappableSprite);
addChild(myTouchSprite);
}
public function tappedThis(event:TouchEvent){
trace("You tapped this");
}
}
}
I should also add that this will only work on a screen/device that triggers TouchEvents(if you're clicking myTappableSprite with a mouse it won't trigger the event).

Add a movieclip on a 3d cube and have interaction with it

I created a rotating cube using the below code and add bitmaps as sides of the cube.
I was wondering if it's possibly to assign movie clips as sides of the cube so I can have some interaction with them.
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import org.papervision3d.cameras.CameraType;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
var cube : Cube;
var basicview : BasicView = new BasicView(640, 480, true, false, CameraType.FREE);
addChild(basicview);
var logo : Logo = new Logo( 0,0); //Bitmapdata (jpeg) exported as class Logo from library
var matFront : BitmapMaterial = new BitmapMaterial(logo);
var matBack : BitmapMaterial = new BitmapMaterial(logo);
var ml : MaterialsList = new MaterialsList();
ml.addMaterial(matFront, "front");
ml.addMaterial(matBack, "back");
ml.addMaterial(new ColorMaterial(0x551F92),"right");
ml.addMaterial(new ColorMaterial(0x431872),"bottom");
ml.addMaterial(new ColorMaterial(0x341359),"top");
ml.addMaterial(new ColorMaterial(0x7429C7),"left");
cube = new Cube(ml,200,200,200,5,5,5);
basicview.scene.addChild(cube);
basicview.camera.fov = 20;
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event) : void
{
cube.yaw((320-mouseX)*0.01);
cube.pitch((240-mouseY)*0.01);
}
basicview.singleRender();
}
UPDATE
I add this
var matFront : MovieMaterial = new MovieMaterial(new MV(),false,true);
matFront.interactive = true ;
MV is just a square with this code
package Scripts {
import flash.display.MovieClip;
import flash.events.*;
public class MV extends MovieClip
{
public function MV( )
{
buttonMode = true;
addEventListener(MouseEvent.CLICK,traceFunction);
}
private function traceFunction(ev:MouseEvent) {
trace("clicked");
}
}
}
Why I have not any interaction when I click the side with the movie clip?
UPDATE 2
Ok I find it
I had to add this line
basicview.viewport.interactive = true;
But why I have interaction even when the side of the movieclip isn't visible.
How can I avoid it?
Yes it is possible, you have to create Materials from the MovieClips you want to use, to do just that take a look at the classes MovieMaterial and MovieAssetMaterial inside the papervision3d materials Package.

creating button

I managed to put texts on screen now I want to show the text after the button is clicked
but the button does not appear.
Here is the code.
package
{
import flash.events.MouseEvent;
import flash.media.Camera;
import mx.controls.Button;
import flash.display.Sprite;
import flash.text.TextField;
public class test2 extends Sprite
{
private var tField:TextField;
public function click(e:MouseEvent):void
{
tField = new TextField();
tField.text="ffff";
addChild(tField);
}
public function test2():void
{
var aa:Button=new Button();
aa.label="deneme";
aa.x=100;
aa.y=200;
aa.addEventListener(MouseEvent.CLICK, click)
}
}
}
You need to alter the test2 function to actually add the button to the stage:
addChild(aa);
You forgot to add the Child to the stage. After setting the position you still need to do addChild(aa);
public function test2():void
{
var aa:Button=new Button();
aa.label="deneme";
aa.x=100;
aa.y=200;
aa.addEventListener(MouseEvent.CLICK, click)
addChild(aa);
}