Why can't I addChild() in actionscript 3? - actionscript-3

probably a really easy question but I've been trying to figure this out for a while...
I'm trying to add a child to the stage to represent the game score like so:
var gameScoreField:TextField = new TextField();
var gameScore:int = 0;
public function Game()
{
// add score
gameScoreField.text = "Score: " + gameScore;
gameScoreField.embedFonts = true;
var format:TextFormat = new TextFormat();
format.font = "Tahoma";
gameScoreField.setTextFormat(format);
addChild(gameScoreField);
}
But when I run the game it isn't showing on the stage. I have no errors and no warnings.
Why is this? Thanks for your help!

Public courtesy to get this off of the unanswered list. Asker or answerer are welcome to post their own answer instead of accepting this one, if they want.
The asker needed to embed the Tahoma font, or set embedFonts to false.

Related

EventListener never get's hit

I'm ripping my hair off at the moment! Now hoping that one of you guys can help me solving my (now two) problem(s).
First one.
I've got the following code:
private var tmpLoader:Loader = new Loader();
private function myFunction():void {
tmpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
tmpLoader.load(new URLRequest(front.url));
}
private function onLoadComplete(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var loadedBitmap:Bitmap = loaderInfo.content as Bitmap;
var sprite:Sprite = new Sprite();
sprite.addChild(loadedBitmap);
addChild(sprite);
sprite.x = 100;
sprite.y = 200;
}
I should also mention that the front.url is equal to a local file path to an image on my computer. Like: "file:///Users/bob/Desktop/potrait.jpg"
My first problem is; why doesn't my onLoadComplete get's hit? Could it have something to do with the url/file path is passed as an argument to URLRequest? Or what could it be?
My second problem revolves hair loss and will also be solved if my first problem gets solved ;-)
Thanx!
first load your content than go for eventListener
just like this:
tmpLoader.load(new URLRequest(front.url));
tmpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
and make sure that there is no problem with your URL address
Try adding Event.COMPLETE for tmpLoader instead.

Loading images using the URLRequest

recently started to learn ActionScript 3 and already have questions.
question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:
var loader:Loader=new Loader();
var urlR:URLRequest=new URLRequest("Image.jpg");
public function main()
{
loader.load(urlR);
var h:Number = loader.height;// here instead of the width of the image of h is set to 0
// And if you do like this:
DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below
// 256 is displayed, as necessary
}
private function DrawText(text:String, x:int, y:int):void
{
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = true;
txt.border = true;
txt.backgroundColor = 0xff000000;
var tFor:TextFormat = new TextFormat();
tFor.font = "Charlemagne Std";
tFor.color = 0xff00ff00;
tFor.size = 20;
txt.x = x;
txt.y = y;
txt.text = text;
txt.setTextFormat(tFor);
addChild(txt);
}
Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.
Well it's simple.
Flash is focused on the Internet, hence such problems.
If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach.
Yeah plus still highly dependent on the size of the file that you read.
Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.
You need to wait for your image to load to be able to get values out of it.
Attach an eventListener to your URLLoader.
var urlR:URLRequest = new URLRequest("Image.jpg");
loader.load(urlR);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
function loader_complete(e:Event): void {
// Here you can get the height and width values etc.
var target_mc:Loader = evt.currentTarget.loader as Loader;
// target_mc.height , target_mc.width
}
Loader

Restarting sound files in AS3

stop();
var mySound:Sound = new MyFavSong();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
myChannel = mySound.play();
pause_btn1.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void
{
lastPosition = myChannel.position;
myChannel.stop();
}
play_btn1.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void
{
myChannel = mySound.play(lastPosition);
}
I am trying to make a restart button however everything I have read is not correctly working on bringing it back to the start of the sound. I am not really great with flash so I made it as simple as I could. Any and all help would be great.
Your code is fine and that's the correct way to implement a pause/play method in as3.
That means the problem is elsewhere. Print lastPosition at the start of your onClickPlay function. Is it printing something other than 0?
Take this as a comment. This thing only lets me post answers.

AS3 multiple textfields made easy

I am working on a Results page for my game as well as upgrade page and looking for an easy way to do many textfields. I have a format for my text that takes care of font, colour, and size, but looking for an easy way to do the width and height of textfields to increase all at the same time.
I have been informed about a "with" keyword that may work but do not understand how to implement this within my program and essentially want to shorten my results class if possible.
Thank you,
The best way would be to create a custom function for generating textfield.
The example can be found in the livedocs itself.
So something like the following should suffice :
private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x;
result.y = y;
result.width = width;
result.height = height;
return result;
}
You may also set a default value to each attribute in the function.
private function createCustomTextField ( x:Number= <Default Value>, ...
Use it to add a textfield inside the container form.
var container:Sprite = new Sprite(); // New form container
container.addChild(createCustomTextField (20,20,50,50)); // Text Filed 1
container.addChild(createCustomTextField (20,50,50,50)); // Text Filed 2
addChild(container); // Add to current class
You may want to modify the function to accept a name so that each variable can be accessed later.
As far as I am aware, you can't use a "with" keyword to target multiple objects. Here's the documentation for it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#with
What I've done in the past is just make an array of all the targets, and then write a loop to apply properties to each:
var textFormat:TextFormat = generateMyCustomTextFormat();
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
//...
var textField3:TextField = new TextField();
var targets:Array = [textField1, textField2, textField3];
for(var i:int=0; i<targets.length; i++)
{
targets[i].defaultTextFormat = textFormat;
targets[i].width = 250;
//...
}

AS3 seeing every score

I have recently taken up programming and encountered a problem when it comes to score display. The score has not problem incrementing and displaying it is just that as the score updates it does not remove the last score. After a dozen frames I have a jumble of scores displayed. I have spent a few days google searching to see if I could find any type of answer but not seen a problem similar to this.
My Code:
public function balldistance(event:Event){ // function called on ENTER_FRAME in order to update the distance of the ball object
var txt:TextField = new TextField();
txt.text = "Distance: " + String(balldist);
txt.x = 25;
txt.y = 25;
addChild(txt);
trace(balldist); // I added this line in my code for troubleshooting purposes just so I could see the balldist augment.
balldist += Ball5.dx; // I am having the score(balldist) augment based on the distance the ball has traveled from its starting point.
}
A friend of mine suggested a removeChild(txt) but when i tried this it did not show the score updating.
Thank you
It looks like you are creating a new txt:TextField EVERY time ENTER_FRAME is triggered.
Try declaring/initializing it once, outside of that listener function:
var txt:TextField = new TextField();
txt.x = 25;
txt.y = 25;
addChild(txt);
Then on enter frame reference the SAME txt TextFeild instance, instead of making a new one over and over again:
public function balldistance(event:Event){
txt.text = "Distance: " + String(balldist);
balldist += Ball5.dx;
}