Health Bar not 100% full - actionscript-3

I have a health bar that, on entering the frame, it decreases slightly and isnt full. How can I fix this? The "Should be" represents what I want it to look like. The "What it currently is" represents the problem I'm having.
Should be:
What is currently is:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.text.Font;
import flash.events.MouseEvent;
import flash.text.TextDisplayMode;
import flash.text.TextExtent;
import flash.text.TextField;
import flash.text.TextFormat;
import fl.motion.easing.Back;
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.ui.Mouse;
import flash.events.TimerEvent;
public class healthBar extends MovieClip
{
public var maxHP:int = 670;
public var currentHP:int = maxHP;
public var percentHP:Number = currentHP / maxHP;
public function healthBar()
{
updateHealthBar();
}
public function updateHealthBar()
{
percentHP = currentHP / maxHP;
scaleX = percentHP;
}
}
}

If you have print statement that outputs the percentHP variable, does it, in fact, display the correct value? 1 in this case, I believe.
You could also manually set scaleX to 1 and see if that still yields a non-filled bar.
Depending on the results of the two tests, you can narrow the issue down to how scaleX is being used, or how percentHP updates.

Related

1120:access of undefined property playWithTouch

I think my code is ok, but for some odd reason it doesn't work properly.
I am new to this AS3 but trying my best to make it work.
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.display.NativeWindow;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.AccelerometerEvent;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.system.System;
import flash.desktop.SystemIdleMode;
import flash.sensors.Accelerometer;
import flash.utils.Timer;
public class Shooter extends MovieClip {
private var welcomeScreenMC: Welcome = new Welcome();
public function Shooter() {
// constructor code
stage.addChild(welcomeScreenMC);
welcomeScreenMC.x = stage.stageWidth / 2;
welcomeScreenMC.y = stage.stageHeight / 2;
welcomeScreenMC.buttonMC.addEventListener(MouseEvent.CLICK, playWithTouch);
}
}
}

as3 Drawing using BitmapData

I am extremely new to actionscript (or any code for that matter) and am having some trouble undertsanding why my code wont work. I am trying to create a small drawing application using BitmapData - there are no errors when I run the code but it doesn't do anything at all.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
var canvas:BitmapData = new BitmapData(100, 100, false, 0x009900);
stage.addEventListener(MouseEvent.MOUSE_DOWN,draw)
function draw(){
canvas.setPixel(mouseX, mouseY, 0x000000);
}
Hopefully this isn't too silly of a question - but if someone can guide me through why this wouldn't work or give me a working example (would be even better) I would be very grateful
You have to add the bitmapdata to the displayObject you are on (it should be a sprite)
private var bitmap:Bitmap;
//on your init function
bitmap = new Bitmap(canvas);
this.addchild(bitmap);
stage.addEventListener(MouseEvent.MOUSE_DOWN,draw)
function draw(e:MouseEvent)
{
bitmap.bitmapdata.setPixel(e.localX, e.localY, 0x000000);
}

New Tween not working correctly

A bit confused as to where Im going wrong here
I have a dynamic text field, called myText.
Im running the below code:
package {
import fl.transitions.Tween;
import fl.transitions.easing.Elastic;
import flash.display.MovieClip;
import fl.transitions.easing.*;
import flash.text.TextField;
import flash.text.TextFormat;
public class video extends MovieClip {
public function video() {
var fmt:TextFormat = new TextFormat();
var letterTween:Tween = new Tween(fmt, "letterSpacing", Elastic.easeInOut, 6, 15, 2, true);
myText.setTextFormat(fmt);
}
}
}
I know its targeting the text as the text letter spacing is set to 6 when running, but nothing happens, I dont get me nice tween to 15 letter spaces(ing)
where am I going wrong?
thanks
Andrew
The reason you don't see any updates is because when TextFormat properties change, you have to reapply the TextFormat. All you need to do is listen for updates from the Tween and apply it there.
My example was tested on the timeline in CS5, go ahead and amend your class accordingly.
import fl.transitions.Tween;
import fl.transitions.easing.Elastic;
import flash.display.MovieClip;
import fl.transitions.easing.*;
import flash.text.TextField;
import flash.text.TextFormat;
import fl.transitions.TweenEvent;
var fmt:TextFormat = new TextFormat();
var letterTween:Tween = new Tween(fmt, "letterSpacing", Elastic.easeInOut, 6, 15, 2, true);
letterTween.addEventListener(TweenEvent.MOTION_CHANGE, onMotionChanged);
myText.setTextFormat(fmt);
function onMotionChanged(event:TweenEvent):void{
myText.setTextFormat(fmt);
}

Actionscript 3 - adjust the z-index of a textfield?

I programmatically created a textfield and I added it to the stage. I placed the textfield close to a dropdown menu that expands when a user hovers over it. Whenever I hover over the drop down menu, some options are hidden because the textfield behaves as if it has a CSS z-index greater than the css z-index of the drop down menu.
I do not have access to the code that created the drop down menu. How do I make it so that my textfield is behind the drop down menu?
Notes the reason I can't programmatically access the dropdown is because I don't have access to the code of the main flash object, whcih is hosted by a 3rd party company. I'm building a plugin. The remote server looks for plugins on my computer and hooks it in.
Sample Code
package {
import flash.system.Capabilities;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import com.brightcove.api.APIModules;
import com.brightcove.api.CustomModule;
import com.brightcove.api.components.ComboBox;
import com.brightcove.api.dtos.VideoDTO;
import com.brightcove.api.events.MediaEvent;
import com.brightcove.api.events.BEMLMouseEvent;
import com.brightcove.api.events.PropertyChangeEvent;
import com.brightcove.api.events.ExperienceEvent;
import com.brightcove.api.modules.ExperienceModule;
import com.brightcove.api.modules.VideoPlayerModule;
import com.brightcove.api.modules.SocialModule;
public class CaptionPlugin extends CustomModule {
private var mVideoPlayer:VideoPlayerModule;
private var mExperienceModule:ExperienceModule;
private var mLanguageSelector:ComboBox;
private var mSocialModule:SocialModule;
override protected function initialize():void {
// Initialize Brightcove Modules
mVideoPlayer = player.getModule(APIModules.VIDEO_PLAYER) as VideoPlayerModule;
mSocialModule = player.getModule(APIModules.SOCIAL) as SocialModule;
mExperienceModule = player.getModule(APIModules.EXPERIENCE) as ExperienceModule;
st = mExperienceModule.getStage();
var txtCaptionField:TextField = new TextField();
txtCaptionField.textColor = 0xFDC10D;
txtCaptionField.selectable = true;
st.addChild(txtCaptionField);
// Init drop down menu for captions
if(!mLanguageSelector)
{
mLanguageSelector = mExperienceModule.getElementByID("closedCaptionsCombo") as ComboBox;
}
}
}
You can use swapChildren();
swapChildren(dropdown, textfield);
This will swap the dropdowns index with that of the textfield
You can also set the index of your text field using
setChildIndex(textfield, 0);
This will set your textfield to index 0, pushing everything else above it.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#setChildIndex%28%29

How to access dynamic text field on stage from within the document class

I'm trying to access a text input that I've already placed on the stage (inside a movie clip) but with no luck.
I've defined an instance name for this dynamic text field which is currentUserCount
I've got something like this set up in the document class actionscript file:
package {
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class myProject extends Sprite {
public function myProject() {
// Trying stuff like
trace(currentUserCount);
trace(movieClipName.currentUserCount);
trace(root.currentUserCount);
}
}
}
What am I missing?
When I run this I get:
1120: Access of undefined property currentUserCount.
1120: Access of undefined property movieClipName.
1119: Access of possibly undefined property movieClipName through a reference with static type flash.display:DisplayObject.
1120: Access of undefined property currentUserCount.
If all of the movieclips on the stage had the same nested clip inside of it, you could also just reference the inner clips like so:
for (var i=0; i<stage.numChildren; i++){
var mc = stage.getChildAt(i)
mc.subClip.play()
}
Make sure the MovieClip and the TextField have instance names in your .fla file.
// MovieClip instance name: mc;
// Textfield instance name: tf;
Then in your document class:
package {
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class myProject extends Sprite {
public function myProject() {
mc.tf.text = "Text you wanna see";
trace(mc.tf); // [Object TextField]
}
}
}
My problem was that I was referencing a movie clip within a movie clip and I didn't know I had to create an instance name for each one and work my way in code through each one.
I thought the instance names were a part of a global namespace.
So I was doing something like:
myMovieClip.play();
when I should have been doing:
mainMovieClip.subMovieClip.myMovieClip.play();