how to connect a number variable to dynamic text in actionscript 3.0? - actionscript-3

i know this might be simple but i have been searching everywhere for a fix but i just cannot find it!
i want to make something like a health #, so when you press whatever button the dynamic text # will go up or down. on my test project i have two layers, the first with the following code
var hp:Number = 100;
health.text = String hp;
hp being the variable, and health being the dynamic text. then i have the next layer with the button with:
function button(e:MouseEvent):void
{
hp -= 10;
}
without that second chunk of code, the dynamic text will appear, but once that is added it will disappear and the button is function-less.
how do i make this work??? once again sorry if this is a dumb question, i'm just very stumped.

The accepted answer is good, but I wanted to point out that your original code was actually very close to being correct, you just needed parenthesis:
health.text = String(hp);
For most objects String(object) and object.toString() has the same effect, except that object.toString() throws an error if object is null (which could be desirable or undesirable, depending on what you expect it to do).

This is not correct:
health.text = String hp;
use:
health.text = hp.toString();
and:
function button(e:MouseEvent):void
{
hp -= 10;
health.text = hp.toString();
}

Related

New to ActionScript3, Making calculator and stuck

first time here on stackoverflow and first time scripting in flashCS6.
ill get down to it - the only lang ive done is html and a bit of css. I tried learning java, but gave up since i realised im making flash games so might as well just do AS3. Its pretty similar and not at all at the same time.
As my first original program (i did a tutorial of pong from a website before, got to know a bit about functions and event handlers[http://as3gametuts.com/2011/03/19/pong-1/]), im trying to create a calculator, and what want to know is how i can return the values from two input fields, put them into a logic calculator (say input a is 1 and input b is 2, and there are four functions, each attached to an event listener for the 4 mathematical operations, and i press addition so the calculator goes 2+1=3)
main question here, how do i get the outut text field to display the answer. In java i just used system.out.println(inputA + inputB).
Here i tried to do out.text = ( a + b) (where out is output , a is input and b is input 2)
Here is the code i have so far:
a is input 1, b is input 2
Out is output
and mul, add, sub and div are symbols containing dynamic test fields with instance names adn, sub, mul and div respectively. The symbol instances are the same as the test instances) Ex: i have a text field that says addition, its instance name is adn, then i convert it to a symbol and make its instance name adn as well.
a.text.restrict = "0-9";
b.text.restrict = "0-9";
mul.addEventListener(MouseEvent.CLICK, output);
adn.addEventListener(MouseEvent.CLICK, addition);
sub.addEventListener(MouseEvent.CLICK, subtraction);
div.addEventListener(MouseEvent.CLICK, division);
a.addEventListener(TextInput,input);
b.addEventListener(TextInput,input);
function output ():void
{
out.text=("test to see if output works")
}
function input (e:TextInput)
{
}
function multiplication (e:MouseEvent)
{
}
function addition (e:MouseEvent)
{
}
function subtraction (e:MouseEvent)
{
}
function division (e:MouseEvent)
{
}
thanks guys, and cheers! Also, ill appreciate if anyone can link me to a good video or text tutorial (series) for AS3 introduction. My main focus is to be making PC games and not apps, so keep that in mind.
Check This Out
Also, don't forget to convert value to string, that may be neccessary:
out.text = String(a + b);
Since a text field will give you the input typecast as a string you will need to type cast them to type Number or type int before you can do any kind of math function on them.
And if you want to create a more complex calculator I would suggest you read up on the Math class
function subtraction (e:MouseEvent)
{
var result:Number = Number(a.text) - Number(b.text)
out.text = String(result)
}

Creating a maze in actionscript3

I4d like to create a maze in actionscript 3 but without drawing it because a lot of people are saying that timeline code and drawing things is bad so i'd like to create it with an array. I've searched on google and looked over a lot of tutorials, all doing it differently but not one of them uses classes and all that stuff and I'd really like to do it. I have the idea of how to do it, using an array filled with different numbers of characters if there's a wall, nothing etc... And i know how to draw the block with the graphics things then put a if loop and if the number is 0 put nothing if the number is 1 create the block and place it, but then i'm a bit lost on HOW to make the block appear at the same spot where there is a 1 in the array, I looked at tuts where they did something with rows but I couldn't really understand it clearly.
And also I'm not sure if i have to create a new class for the block, and what do I have to put in this class if i do create it? Do i need to create the block in the class, or outside of it? =/
If someone knows what I mean then all help is welcome.
If you need more details please tell me, sorry if it's confused. =3
It looks like your best bet, your path of least resistance in the long run, may lie in doing a little bit more study and some smaller programs before embarking on this. However if you want a 2D maze made with an Array, you could just do this:
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = 1;
m_arrMaze[0][1] = 1;
.
.
.
m_arrMaze[24][24] = 3;
.
.
.
m_arrMaze[49][49] = 0;
}
This is because you seemed to mention using an array and setting its elements to certain int values to denote what each little spot or room or whatever in the maze is or has. The reason a lot of tutorials may not use a whole lot of classes is because, if this is all you're doing with it, you really don't need too many different classes to denote the stuff in the maze. Just instead of using hard-coded int values, go ahead and put them in constants at the top of your maze class:
private static const EMPTY_SPACE:int = 0;
private static const WALL:int = 1;
.
.
.
private static const PLAYER:int = 3;
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = WALL;
m_arrMaze[0][1] = WALL;
.
.
.
m_arrMaze[24][24] = PLAYER;
.
.
.
m_arrMaze[49][49] = EMPTY_SPACE;
}
If each type of contents within the maze is liable to have a whole different set of nouns, verbs, and adjectives associated with it, instead of just being a different type of marker of where something's at like in the examples above, and if the program is going to do a lot of different things with those contents, that's when you want to use a whole bunch of different classes. Hopefully this will get you started.

Counter with "for" displaying on a dynamic textfield

im very new to as3 so i would appreciate any help.
Im trying to make a counter only using the command "for".
im counting on this from 1 to 1000 in steps of 20.
the next step i want to make is to display on the output tab
i already know i can make it with "trace();", but i also want this to be displayed on
the main .swf window, im trying using a dynamic text-field which i named "dyna"
The problem is that, it is only displaying the last number. "1000" or changing very fast that i barely notice, and the last one remains.
var i:int;
for (i = 1; i < 1001; i+=20)
{
trace(i);
//dyna is the name of my dynamic textfiled
dyna.text = i.toString();
//dinamico.text = String(i);
}
-Is there any way to record all the numbers on my dynamic textbox, something like [1,20,40,60,....] horizontally or vertically.
-Or maybe someway to run this from a button step by step.
like [click, 20; click, 40; click 60.....]
Thanks in advance
var i:int;
var str:String="1";
for (i = 20; i < 1001; i+=20)
{
str=str+","+i;
}
dyna.autoSize = TextFieldAutoSize.LEFT;
dyna.text=str;
Output
1,20,40,60,80,100,120,140,160...
Hope it helps
To run this from the button step by step you need a button, a listener attached to the button, a counter available to both button and text field, and a bit of code. The button has to be somewhere on the stage or in your asset, and named somehow, so you can address it by the name. Here it's named yourButton:
var counter:int=0;
yourButton.addEventListener(MouseEvent.CLICK,updateDyna);
function updateDyna(e:MouseEvent):void {
counter+=20;
if (counter>1000) counter=1000;
dyna.text=counter.toString();
}
Here you are, click - 20, click - 40, etc., up to 1000.

Can I just use e4x or do I need regex also?

I have some XML that needs to be manipulated into a string to render some instructions. The text looks like this
<?xml version="1.0" encoding="UTF-8"?>
<instructions id="detection" version="1.0">
<instruction task="detection">
<phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>
<phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>
<phrase type="real">You are now going to do a test.<nl/><nl/></phrase>
<phrase>As soon as the card turns face up:<nl/><nl/></phrase>
<phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>
<phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>
<phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>
</instruction>
</instructions>
Now, all I need to do is the following
Replace all <nl/> with \n
Replace all <ts/> with \t
Conditionally select practice or real, probably by removing the other
Remove all XML bits remaining to result in a string.
so lets say I want the practice version of this, I should end up with
HAS THE CARD TURNED OVER?\n\n\n
you are now going to do a practice.\n\n
As soon as the card turns face up:\n\n
\t\tPress YES.\n\n
Go as fast as you can and try not to make any mistakes.\n\n
If you press YES before a card turns face up, you will hear an error sound.
Now, I have the opportunity to change the structure of the XML if the current form isn't ideal for this, but what I'm not sure is if I can do all of the above with e4X or I need to also use regex's? Some examples would be great.
It can be done with E4X, probably not as elegantly as regex.
Here's an example of replacing <nl> with "\n" using E4x:
package
{
import flash.display.Sprite;
public class e4xStuff extends Sprite
{
private var srcxml:XML;
public function e4xStuff()
{
srcxml = new XML( '<instructions id="detection" version="1.0">' +
'<instruction task="detection">' +
'<phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>' +
'<phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>' +
'<phrase type="real">You are now going to do a test.<nl/><nl/></phrase>' +
'<phrase>As soon as the card turns face up:<nl/><nl/></phrase>' +
'<phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>' +
'<phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>' +
'<phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>' +
'</instruction>' +
'</instructions>');
processNode(srcxml);
trace(srcxml);
}
private function processNode(xml:XML):XML
{
//replace <nl/> with \n
if(xml.name() == "nl")
{
return new XML("\n");
}
var children:XMLList = xml.children();
if(children.length() == 0)
{
return xml;
}
//remove the children
xml.setChildren(new XMLList());
//put the children back, one-by-one, after checking for <nl/>
for(var i:int=0; i<children.length(); i++)
{
xml.appendChild(processNode(children[i]));
}
return xml;
}
}
}
A list of E4X methods is posted at http://wso2.org/project/mashup/0.2/docs/e4xquickstart.html
You can check for practice or real using xml.#type

Re-stacking MovieClips in an Array

I was trying to make a similar thing with the game SameGame (ie. the block above the removed blocks fall downward). Before trying this with an Array that contains MovieClips, this code worked (tried it with int values). With MovieClips on the array, it seems not working the same way.
With int values, example:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After: 1,2,3,4,6,7,8,9,10
But with MovieClips:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After; 1,2,3,4
// Assume the numbers are movieclips XD
Basically, it strips everything else, rather than just the said block >_<
Here's the whole method. Basically, two extra arrays juggle the values above the soon-to-be removed value, remove the value, then re-stack it to the original array.
What could be wrong with this? And am I doing the right thing for what I really wanted to emulate?
function popUp(col:uint, row:uint)
{
var tempStack:Array = new Array();
var extraStack:Array = new Array();
tempStack = IndexArray[col];
removeChild(tempStack[0]);
for(var ctr:uint = tempStack.length-(row+1); ctr > 0; ctr--)
{
removeChild(tempStack[ctr]);
extraStack.push(tempStack.pop());
trace(extraStack);
}
tempStack.pop();
for(ctr = extraStack.length; ctr > 0; ctr--)
{
tempStack.push(extraStack.pop());
//addChild(tempStack[ctr]);
}
IndexArray[col] = tempStack;
}
PS: If it's not too much to ask, are there free step-by-step guides on making a SameGame in AS3 (I fear I might not be doing things right)? Thanks in advance =)
I think you just want to remove an element and have everything after that index shift down a place to fill what you removed. There's an inbuilt function for this called splice(start:uint, length:uint);
Parameters:
start - the index to start removing elements from
length - the amount of elements to remove
var ar:Array = ["hello","there","sir"];
ar.splice(1, 1);
ar is now -> ["hello", "sir"];
As per question:
Here's an example with different types of elements:
var ar:Array = [new MovieClip(), "some string", new Sprite(), 8];
ar.splice(2, 1);
trace(ar); // [object MovieClip], some string, 8
And further example to display the indexes being changed:
trace(ar[2]); // was [object Sprite], is now 8