Creating text in AS3 - actionscript-3

I followed this tutorial in order to get a better understanding of classes found here.
I did everything correctly up to step 10, but when I went to export the SWF I got a blank screen. I get an error in the compiler that says "expecting identifier before dot."
I don't know what this means but you can download my files and take a look and hopefully give me an answer, thanks!
Here is my code :
package {
import flash.display.MovieClip;
import flash.text.TextField;
import.text.TextField;
public class documentclass extends MovieClip
{
public function documentclass()
{
// constructor code
var tf:TextField = new TexfField();
addChild(tf);
tf.text = "Hello Wrold";
}
}
}

Your error is fired by this line, there is a dot (.) after import :
import.text.TextField;
which even you don't need because you've already imported the TextField class.
Then you have a little typing error in this line :
var tf:TextField = new TexfField();
which should be :
var tf:TextField = new TextField(); // new TextField, with "t" not "f"
so your final code can be like this :
package {
import flash.display.MovieClip;
import flash.text.TextField;
public class documentclass extends MovieClip
{
public function documentclass()
{
// constructor code
var tf:TextField = new TextField();
addChild(tf);
tf.text = "Hello Wrold";
}
}
}
Hope that can help.

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

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: Display data(integer) from an swf to another swf through class

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

1136: Incorrect number of arguments. Expected 0. on var ourShip:ship = new ship(stage);

Hi guys i am new to programming, I was following a tutorial on As3 Flash Games
I came across a code in which it was written
var ourShip:ship = new ship(stage);
But it gave an error
1136: Incorrect number of arguments. Expected 0.
I believe that the error it right but then what about the comments that the tutorial was awesome !
The as file which includes this code has in it
package
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
//our Engine class it extends MovieClip
public class engine extends MovieClip
{
//our constructor function. This runs when an object of
//the class is created
public function engine()
{
//create an object of our ship from the Ship class
var ourShip:ship = new ship(stage);
//add it to the display list
stage.addChild(ourShip);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
}
}
}
Also to tell that i changed the senocular package from
package come.asgamer.....{}
to
package utils{}
I did this as i made a single folder named utils which has senocular action script in it
I thought mabye the problem related to this
By the way if i change
var ourShip:ship = new ship(stage);
to
var ourShip:ship = new ship();
There is no result which means flash player displays the ship in middle as it is said
Ship action script file :
package
{
import flash.display.MovieClip;
import utils.KeyObject;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.events.Event;
public class ship extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
public function Ship(stageRef:Stage)
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
if (key.isDown(Keyboard.LEFT))
x -= 2;
else if (key.isDown(Keyboard.RIGHT))
x += 2;
if (key.isDown(Keyboard.UP))
y -= 2;
else if (key.isDown(Keyboard.DOWN))
y += 2;
}
}
}
Note : I completed the tutorial
Capitalize ship; that's what they're doing in the tutorial. Try that and see if it works.
It is possible you are using a different class than the one you think you're using. The class they provided is Ship, not ship, and in the code they provided for Ship, the constructor actually does have an a parameter:
package com.asgamer.basics1{
import flash.display.MovieClip; import flash.display.Stage;
import com.senocular.utils.KeyObject;
import flash.ui.Keyboard;
public class Ship extends MovieClip {
private var stageRef:Stage;
private var key:KeyObject;
public function Ship(stageRef:Stage)
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);
}
}
}
If capitalizng ship doesn't work, you're moving in the right direction, but there may be another piece to the puzzle.

Event listener on button not working

So, my menu for my game is in a separate .fla file and I have used a loader like so to load the menu into my game:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
import caurina.transitions.Tweener;
public class Main extends MovieClip {
public static var gameLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public var gameTime:int;
public var levelDuration:int;
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
var myMenu:Loader = new Loader();
var url:URLRequest = new URLRequest("Menu.swf");
myMenu.load(url);
myMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, menuLoaded);
function menuLoaded(event:Event):void
{
menuLayer.addChild(myMenu.content);
}
playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to remove the menu (menuLayer.removeChild?)
// Code here to start timers etc
}
I set instance names for my buttons but when I try to do something like menuLayer.playBtn.addEventListener(MouseEvent.CLICK, startGame);, I get messages saying Access of undefined property playBtn.
Now, I double checked on my Menu.fla and I definitely gave the button an instance name of playBtn but it's not working. Any help please? Might be something really obvious I've missed but I'm not sure what.
EDIT: Trying it another way (Converting the menu to a movieclip) but not 100% sure how to do it exactly. The code I have is:
public class Main extends MovieClip {
var mainMenu:menuMain = new menuMain;
// Other variables
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
mainMenu.addEventListener(Event.COMPLETE, menuLoaded);
}
function menuLoaded(event:Event):void
{
//var mainMenu:LoaderInfo = event.currentTarget as LoaderInfo;
//var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
// now you can actually add the listener, because the content is actually loaded
mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to execute timers etc.
}
My guess is that you think that when you go menuLayer.addChild(myMenu.content) that menuLayer suddenly becomes an instance of menu.swf That is not the case. It becomes a child of menuLayer.
Try this :
menuLayer.addChild(myMenu.content);
var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
trace (menuInstance.playBtn);
This code assumes that you have nothing else added to menuLayer and in that case your menu.swf content would be the only child on the display list of menuLayer.
I am also assuming that menu.swf's contents are a MovieClip.
If my assumptions are wrong, this may not work.
I also noticed that you have your menuLoaded method inside your constructor. Not a good idea. Especially since the next line is expecting playBtn to exist and the menu hasn't even been loaded.
Try something like this :
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
var myMenu:Loader = new Loader();
var url:URLRequest = new URLRequest("Menu.swf");
myMenu.load(url);
myMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, menuLoaded);
}
function menuLoaded(event:Event):void
{
var myMenu:LoaderInfo = event.currentTarget as LoaderInfo;
menuLayer.addChild(myMenu.content);
var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
// now you can actually add the listener, because the content is actually loaded
menuInstance.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to remove the menu (menuLayer.removeChild?)
// Code here to start timers etc
}