ForumsProgramming ForumVariable isn't working.

18 4672
Xinito
offline
Xinito
111 posts
140

I just bought Flash and i'm trying some stuff in Flash building a Game in as2 but if I make a object that needs to add 5 points in the variable it doesnt go to 5 and it's stuck on 0 the whole time. Is it true that variables in as2 dont work for flash CS5?

  • 18 Replies
Xinito
offline
Xinito
111 posts
140

I did in the first frame

good = 100;
bad = 50;

and I made some dynamic text and did var: good and bad. When I test the movie they wont show up..

Darkroot
offline
Darkroot
2,799 posts
1,120

I'm not really sure what you're talking about as2 doesn't require you to declare variable type as in as3.

For as2 you can get away with

var greeting = "Hello, world!" or even
greeting = "Hello, world!"

with as3 you can't you have to be more explicit

var greeting:String = "Hello, world!" but you can get away with
var greeting = "Hello, world!"

but if I make a object that needs to add 5 points in the variable it doesnt go to 5


Object is something entirely different in programming, careful with your word choice.

Is it true that variables in as2 dont work for flash CS5?


Yeah they work but you have to work in an as2 document and not an as3 document, if you want to write as2.

and I made some dynamic text and did var: good and bad. When I test the movie they wont show up..


Not sure you probably set up the textbox wrongly. Why don't you use trace(good); instead of displaying them anyway?
arobegamr
offline
arobegamr
130 posts
540

If I read this correctly, you are referring to entering good and bad into the var field within the dynamic text properties box.

However, it is possible to have the text box display a variable through as2.

For example, you could place a dynamic text box and give it the instance name score_txt

You could have the text box update automatically with each frame, by typing the following into the actions window of the main stage:

score = 0
onEnterFrame = function (){
score_txt.text = score
}


Or, you could have the text box update every time the variable is changed. You could create a movie clip with the instance name "coin_mc" Then type this one the main stage:

coin_mc.onPress = function (){
_root.score += 5
_root.score_txt.text=_root.score
}


If you choose to use the first option, simply remove the "_root.score_txt.text=_root.score" from the second, but use the rest.

I hope this helps to solve your problem

Xinito
offline
Xinito
111 posts
140

@Darkroot, ofc I worked in a AS2 file if I want to script with AS2,

Thanks for the information guys, i will try it.

Xinito
offline
Xinito
111 posts
140

@arobegamr Thanks so much, it works! now I can continue my rpg .

Xinito
offline
Xinito
111 posts
140

arobegamr, it works perfectly but now I want that the coin disappear if I clicked on it, is this possible?

Darkroot
offline
Darkroot
2,799 posts
1,120

Either make it go to the next empty frame in the timeline of the coin movieclip (if you are using one). Or just _root.coin.removeMovieClip(); or something similar, not 100% sure how it works in as2.

Xinito
offline
Xinito
111 posts
140

I tryed that but fixed it with unloadMovie(this);

But I did this script to the coin:

onClipEvent (enterFrame) {
if (_root.char.hitTest(this))
{
_root.score += 5;
_root.vcam.score_txt.text = _root.score;
unloadMovie(this);
}
}

And I made a few more coins but if I hit one I get like 50 score because there 10 coins on my map, how can I fix that I only get 5 score and not all the scores once?

Xinito
offline
Xinito
111 posts
140

Anyone can solve this probleme?

arobegamr
offline
arobegamr
130 posts
540

In response to your removal problem, removeMovieClip() only works with clips created in actionscript, such as using the attachMovie() function.

As for your second problem, this is because you have multiple movie clips with the same instance name. You probably gave the first movie clip an instance name and then copied the clip, without changing it. But for what you are doing, you do not need an instance name at all, so leave the clips unnamed.

Xinito
offline
Xinito
111 posts
140

Thanks, will try that

stijnhero
offline
stijnhero
22 posts
125

try to separete bad and good on the dynamic text?

WhiskeyedJack
offline
WhiskeyedJack
81 posts
1,480

You could always try attaching the instances of your coin movie clip, and holding a reference to each one in an array. If you have a predetermined set of coins, you would have to iterate through each manually and determine its placement. Otherwise you could randomly generate coins within a given set of parameters, then add it to an array with the push() function. If you want to see an example of what I mean, check out the Kongregate tutorial, especially sections 4 and 5 where they talk about adding enemies. You might be able to adapt it to what you are doing here.

WhiskeyedJack
offline
WhiskeyedJack
81 posts
1,480

Just for clarification, the main page for that tutorial is at [url]http://www.kongregate.com/labs[/url].

Xinito
offline
Xinito
111 posts
140

Gonna take a look there.

Showing 1-15 of 18