Why the styleSheet is not working? - actionscript-3

package {
import flash.text.TextField;
import flash.text.StyleSheet;
import flash.text.TextFieldAutoSize;
public class MyTF extends TextField {
public var
color:uint,
size:int
public function MyTF( color:uint, size:int) {
super();
color = color; // setting color and size
size = size;
init(); //initialize
}
private function init():void
{
autoSize = TextFieldAutoSize.LEFT;
var style:StyleSheet = new StyleSheet();
style.setStyle('span', {color: 0xFF0000, fontSize: 24});
styleSheet = style;
htmlText = '<span>test</span>';
//after creating an object of this class the text Style is not changing
}
}
}
Why is the htmlText style not changing after I create an instance of this class? Is there something wrong?

If I'm not wrong understand you this can help you:
style.setStyle('span', {color: 0xFF0000, fontSize: 24}); change to
style.setStyle('span', {color: this.color, fontSize: this.size});.

Related

How do I create an object using code in AS3 and give it a picture as display?

package
{
import flash.events.*;
public class declareImage extends Sprite
{
var ship:Sprite = new Sprite();
public function declareImage()
{
}
}
}
I declared an object.
Now I want to give it a background as a picture from my Compute
Should I use Sprite as data type or something else?
Here is an example. Of course, it's a very simple case (using fixed file name, etc), just to illustrate how to achieve what do you want and provide you the foundation to move on.
A tip, begin a class name with an uppercase letter.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
public class DeclareImage extends Sprite
{
private const IMAGE_URL:String = 'myImage.jpg';
private var ship:Sprite;
private var loader:Loader;
public function DeclareImage()
{
ship = new Sprite();
addChild(ship);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler, false, 0, true);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler, false, 0, true);
loader.load(new URLRequest(IMAGE_URL));
}
private function loadProgressHandler(event : ProgressEvent) : void
{
trace('Loading: ' + Math.round((event.bytesLoaded/event.bytesTotal) * 100) + '%');
}
private function loadCompleteHandler(event:Event):void
{
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgressHandler);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
ship.addChild(loader);
trace('complete');
}
}
}

Not Displaying Nape Body

I had these two in the same Main class. Now there are Main.as and MainChar.as
When compiling doesn't show any errors but it isn't displaying the object either :c
It is the first time I am splitting code into different classes. I just figured what should be in the hero creation class and what should stay in Main.
package
{
import flash.display.Sprite;
import flash.events.Event;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyList;
import nape.space.Space;
import MainChar;
public class Main extends Sprite
{
public var gravity:Number = 600;
public var space:Space = new Space(new Vec2(0, gravity));
public var hero:MainChar = new MainChar();
public function Main():void
{
hero.createMainCharacter(stage.stageWidth/2, stage.stageHeight/2, 50, 50);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void
{
space.step(1 / stage.frameRate, 10, 10);
var bodies:BodyList = space.bodies;
for (var i:int = 0; i < bodies.length; i++)
{
var body:Body=bodies.at(i);
if (body.userData.sprite != null)
{
body.userData.sprite.x = body.position.x;
body.userData.sprite.y = body.position.y;
body.userData.sprite.rotation=(body.rotation*180/Math.PI)%360;
}
}
}
}
}
Hero creator class:
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.shape.Polygon;
import nape.space.Space;
public class MainChar extends Sprite
{
public var space:Space = new Space(new Vec2(0, 600));
public function MainChar():void
{
}
public function createMainCharacter(x:Number, y:Number, width:Number, height:Number):void
{
var mainChar:Body = new Body(BodyType.DYNAMIC);
var mainCharShape:Polygon = new Polygon(Polygon.box(width, height));
mainChar.shapes.add(mainCharShape);
mainChar.position.setxy(x, y);
mainChar.space = space;
var mainCharSprite:Sprite = new Sprite();
mainCharSprite.graphics.beginFill(0x000000);
mainCharSprite.graphics.drawRect( -width/2, -height/2, width, height);
mainCharSprite.graphics.endFill;
addChild(mainCharSprite);
mainChar.userData.sprite = mainCharSprite;
addChild(mainChar.userData.sprite);
}
}
}
You need to add your hero as a child of your Main sprite EG:
public function Main():void
{
hero.createMainCharacter(stage.stageWidth/2, stage.stageHeight/2, 50, 50);
addEventListener(Event.ENTER_FRAME, update);
addChild(hero);
}

Starling not displaying graphic

Hi guys i am new to AS3 and to Starling i am having problem displaying an image in a different class. it does show on my game menu class but i wanted it to show on my play game class.
so i got 3 classes Main that starts the starling that runs GameMenu class.
"GameMenu class"
package {
import starling.display.BlendMode;
import starling.display.Button;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
public class GameMenu extends Sprite {
private var bg:Image;
private var gameLogo:Image;
private var playBtn:Button;
private var rankBtn:Button;
private var settingBtn:Button;
private var inGame:PlayGame;
public function GameMenu ()
{
super ();
this.addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage (event:Event):void
{
this.removeEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
drawScreen ();
}
private function drawScreen ():void
{
bg = new Image(Assets.getAtlas().getTexture(("Background.png")));
bg.blendMode = BlendMode.NONE;
this.addChild(bg);
gameLogo = new Image(Assets.getAtlas().getTexture(("GameLogo.png")));
gameLogo.x = stage.stageWidth/2 - gameLogo.width/2;
gameLogo.y = 30;
this.addChild(gameLogo);
playBtn = new Button(Assets.getAtlas().getTexture("PlayBtn.png"));
playBtn.x = stage.stageWidth/2 - playBtn.width/2;
playBtn.y = 450;
playBtn.addEventListener(Event.TRIGGERED, onPlayClick);
this.addChild(playBtn);
rankBtn = new Button(Assets.getAtlas().getTexture("RankBtn.png"));
rankBtn.x = rankBtn.bounds.left + 60;
rankBtn.y = 600;
rankBtn.addEventListener(Event.TRIGGERED, onRankClick);
this.addChild(rankBtn);
settingBtn = new Button(Assets.getAtlas().getTexture("SettingBtn.png"));
settingBtn.x = settingBtn.bounds.right + 60;
settingBtn.y = 600;
settingBtn.addEventListener(Event.TRIGGERED, onSettingClick);
this.addChild(settingBtn);
}
private function onRankClick (event:Event):void
{
trace("LEADERBOARD BUTTON HIT")
}
private function onSettingClick (event:Event):void
{
trace("SETTING SCREEN BUTTON HIT")
}
private function onPlayClick (event:Event):void
{
playBtn.removeEventListener(Event.TRIGGERED, onPlayClick);
trace("PLAY BUTTON HIT")
gameLogo.visible = false;
playBtn.visible = false;
rankBtn.visible = false;
settingBtn.visible = false;
//bg.visible = false;
inGame = new PlayGame();
}
}
}
this class works perfectly now.
"PlayGame class"
package {
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
public class PlayGame extends Sprite
{
private var bubble:Image;
public function PlayGame ()
{
trace("PlayGame");
super ();
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage2);
}
private function onAddedToStage2 (event:Event):void
{
trace("OnAddedToStage");
this.removeEventListener (Event.ADDED_TO_STAGE, onAddedToStage2);
drawScreen ();
}
public function drawScreen ():void
{
trace("Bubble");
bubble = new Image(Assets.getAtlas().getTexture(("Bubble.png")));
bubble.x = 100;
bubble.y = 100;
this.addChild(bubble);
}
}
}
the bubble image is now showing and i don't know why?
They are too much parenthesis, but this is not your issue
bubble = new Image(Assets.getAtlas().getTexture(("Bubble.png")));
//is less clean/readable than
bubble = new Image(Assets.getAtlas().getTexture("Bubble.png"));
When facing this kind of issues, if starling/air doesn't report any errors, you should try to be sure the display object are currently rendered.
Try to apply a color instead of the texture.
bubble = new Image( Texture.fromColor(64,64,0xff99ff) );
If this code shows a fancy rectangle of 64 pixel/side, you trouble come from your texture atlas or texture. Be sure to have a valided atlas, validates frames, and names.
If this code doesn't show the facy rectangle, your issue doesn't come from the texture atlas or texture, but more possibly from the display list of starling.
And in your case,
I think you should just need to addChild the Ingame in your GameMenu class :
GameMenu :
private function onPlayClick (event:Event):void
{
//... your stuff
inGame = new PlayGame();
this.parent.addChild(inGame);
}
And it should works.

Is it possible to share the text area from one class to another class in as3?

This is the class text.as3
private function showTextArea():void{
textField = new TextArea();
canvas.addChild(textField);
}
This is the code to create the text area,i want to pass the text area to another class(text2.as3).Is it possible?
package{
import fl.controls.*;
import flash.display.*;
public class Text1 {
public var txtA:TextArea;
public var str:String = "text";
public function Text1():void{ }
public function showText(spr:Sprite):void
{
txtA = new TextArea();
txtA.text = str;
spr.addChild(txtA);
}
}
}
and Main class
package {
import flash.display.Sprite;
public class Main extends Sprite{
public function Main():void {
var spr:Sprite= new Sprite();
var txt1:Text1 = new Text1();
txt1.showText(spr);
addChild(spr);
trace(txt1.txtA.text);
}
}
}

Actionscript: add a sprite from a subclass to the display list of its super class?

Say i have these two classes:
MAIN.as
package
{
import flash.display.*; import mx.core.*;
import flash.events.*; import mx.collections.*;
import flash.geom.*; import mx.controls.*;
import flash.text.*; import mx.events.*;
import mx.styles.*;
import mx.containers.*;
public class MAIN extends Sprite
{
public var APPLICATION:Application = Application(Application.application);
public var keyDownText:TextField = new TextField();
public function MAIN()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KEY_DOWN);
addEventListener(Event.ENTER_FRAME,STEP);
new OBJECT_square().CREATE(10,100,1);
}
public function STEP():void {}
public function DRAW():void {}
public function KEY_DOWN(event:KeyboardEvent):void
{
keyDownText.text = "Key code: " + event.keyCode;
this.addChild(keyDownText);
}
}
}
OBJECT_square.as
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends Sprite
{
public var X:int = 0;
public var Y:int = 0;
public var DEPTH:int = 0;
public var SPRITE:Sprite = new Sprite();
public function CREATE(X:int,Y:int,DEPTH:int):void
{
this.DEPTH = DEPTH;
this.X = X;
this.Y = Y;
DRAW();
}
public function DRAW():void
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
}
}
}
Now how is it that I can add the variable SPRITE which is a Sprite in the OBJECT_square class to the display list of MAIN class? I've tried addChild(SPRITE) and super.addChild(SPRITE). If everything works I should see a red square somewhere on the screen but right now its all blank, except for the text drawn in the MAIN class.
Basically I want it so i can just make a new OBJECT_square and it will draw itself without any more instructions from the MAIN class.
Try this:
var obj:OBJECT_square = new OBJECT_square();
obj.CREATE(10,100,1);
addChild(obj);
Or, if you really want to do it in one go, you could try this:
In main
addChild((new OBJECT_square()).CREATE(10,100,1));
And change your draw function to return the square object
public function DRAW():OBJECT_square
{
SPRITE.graphics.beginFill(0xFF0000,1);
SPRITE.graphics.drawRect(X - 10,Y - 10,20,20);
SPRITE.graphics.endFill();
addChild(SPRITE);
return this;
}