Adding things dynamically

Subscribe to Adding things dynamically 18 posts

Sign in to reply


 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

As FlashDevelop does not have a time line/stage to draw and add text boxes and things like that, the only way to add things is by using the code to add them. There are certain things that I have found to play dynamically such as pictures and movie clips, but other objects such as dynamic text boxes, buttons, bars, and audio are not as easy for me to find. Does anyone know how I can insert these things or a website that I can use for these and any more that i encounter for actionscript 3. Thanks.

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

What exactly are you finding so difficult? Most of those things are just the same as pictures/movieclips:

var textfield:TextField = new TextField();
textfield.text = "text";
addChild(textfield);
 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

Well that works if you have one in the library, unless I’m implementing it incorectly.


var scoreText:TextField = new TextField();
scoreText.x = 290;
scoreText.text = “HI”;
addChild(scoreText);

With the exception of music how would you place one that isnt in your library.

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

You don’t need anything in your library. If you’re getting an error it’s probably because you haven’t inported TextField: import flash.text.TextField;

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

i’m not getting an error, just nothing is appearing.

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

I started a new AS3 FlashDevelop project, imported TextField and added this code to the init function:

private function init(e:Event = null):void 
{
     removeEventListener(Event.ADDED_TO_STAGE, init);
     // entry point
     var textfield:TextField = new TextField();
     textfield.text = "text";
     addChild(textfield);
}

It shows a textfield in the top left corner that says text. If that doesn’t work for you there must be something wrong with your fonts or something.

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

I’ll have to see in a few minutes, I’m getting errors elsewhere that I need to fix. Does this work for like music and meters/bars? For music its usually


static var backgroundMusic = new BackgroundMusic();
backgroundMusic.play(0,1000);

but that is when you gave it that class when you gave it properties, but how would you give it the name dynamically.

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

Exactly the same as pictures:

[Embed(source="music.mp3")]
private var BackgroundMusic:Class;

static var backgroundMusic:Sound = new BackgroundMusic();
backgroundMusic.play(0, 1000);
 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

Thanks. So i’ve been working on making a health bard and this is what i got:


graphics.beginFill(0xFFFFFFF);
graphics.drawRect(14.5, -5, 50, 5);
addChild(healthBar);
healthBar.x = 14.5;
healthBar.y = -5;
healthBar.width = 50;
healthBar.height = 5;
healthBar.addChild(hb);
healthBar.scaleX = health / maxhealth;
graphics.lineStyle(1,0×000000);
graphics.moveTo(14.5, -5);
graphics.lineTo(65, -5);
graphics.moveTo(14.5, -5);
graphics.lineTo(14.5, 0);
graphics.moveTo(65, -5);
graphics.lineTo(65, 0);
graphics.moveTo(14.5, 0);
graphics.lineTo(65, 0);

What it does is that it makes a white rectangle at behind everything for the backdrop, it then adds a clip (green health) which is supposed to be what moves [health bar is a movie clip and hb is a graphic(i couldn’t figure out how to fill it, if you know please tell me)] and last it makes the border. The problem is, is that it only attaches the green health bar to the newest ship on the screen, and then if something else appears it disappears and attaches to the new one. Also, it doesn’t move (not as in x y coordinates but scaling) so what’s the problem?

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

If you’re using the drawing API you want to be redrawing rather than scaling:

package{

     import flash.display.Shape;
     import flash.display.Sprite;

     public class HealthBar extends Sprite{
 
         private const BAR_WIDTH:int = 50;
         private const BAR_HEIGHT:int = 5;

         private var bar:Shape;

         public function HealthBar(){

             bar = new Shape();
             bar.graphics.beginFill(0x00FF00);
             bar.graphics.drawRect(0, 0, BAR_WIDTH, BAR_HEIGHT);
             bar.graphics.endFill();
             addChild(bar);

             graphics.lineStyle(1, 0x000000);
             graphics.drawRect(0, 0, BAR_WIDTH, BAR_HEIGHT);
 
         }

         public function update(health:int):void{

             bar.graphics.clear();
             bar.graphics.beginFill(0x00FF00);
             bar.graphics.drawect(0, 0, (health/Ship.MAX_HEALTH) * BAR_WIDTH, BAR_HEIGHT);
             bar.graphics.endFill();

         }

     }

}


package{

     import flash.display.Sprite;

     public class Ship extends Sprite{
    
         public static const MAX_HEALTH:int = 100;

         private var health:int;
         private var healthBar:HealthBar;

         public function Ship(){

             health = MAX_HEALTH;
            
             healthBar = new HealthBar();
             healthBar.y = -20;
             addChild(healthBar);

         }

         public function update():void{

             if(this.hitTest(Enemy.enemyList))
                  healthBar.update(health - 10);

         }

     }

}

or something like that. You can expand this to make your health bar as complex as you want, maybe have it change colour as your health decreases or have it flash when it gets really low. Anything really.

 
avatar for Gamepages Gamepages 75 posts
Flag Post

I’d highly recommend not drawing it like that. use a movieclip.

 
avatar for rarapompoms rarapompoms 184 posts
Flag Post

How would you recommend drawing it and for what reasons?

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post
Originally posted by Gamepages:

I’d highly recommend not drawing it like that. use a movieclip.

I’d be interested to hear any suggestions you may have on making healthbars in actionscript. However, dismissing my ideas without explanation and offering only an extremely vague alternative is not helpful to anyone.

 
avatar for Varilian Varilian 92 posts
Flag Post
Originally posted by Gamepages:

I’d highly recommend not drawing it like that. use a movieclip.

Drawing it like that is less memory-intensive, and is better when it’s scaled.

Anyone can correct me if I’m wrong, but isn’t he better off drawing it like this?

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

SuperMario: When doing it the way you supported, errors arise in the beginFill and end fill stating: src\HealthBar.as(23): col: 18 Error: Call to a possibly undefined method beginFill through a reference with static type flash.display:Shape.

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

Sorry, it’s bar.graphics.beginFill() and bar.graphics.endFill()

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

Thanks man, it works perfectly. Where did you learn all of this? I’ve read most of the Essential Actionscript 3.0 but maybe i’m missing the concepts.

 
avatar for SuperMarioJump SuperMarioJump 303 posts
Flag Post

I just read the documentation on the drawing API. It’s not the most interesting read, but you can discover lots of stuff just by having a browse through the AS3 docs.

Sign in to reply