Help with variables

Subscribe to Help with variables 22 posts

avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

I have a key frame which states that: variable1 = 10
Than a button when pressed states: variable1 -=1
So I press the button and the variable1 is now 9. I go to the next frame and when I go back to the first frame the variable is again 10. Why?

Basic:
Key frame action states variable1 = 10
Button states on press variable1 -=1
I press button. Variable1 = 9.
I go to next frame. I come back to the first frame.
Variable1 is again 10. How can I make it so that the variable is 9 now?

 
avatar for Shake_N_Baker Shake_N_Baker 53 posts
Flag Post

I’d advise you to try and code in classes when possible rather than on the timeline as it is much less messy and much easier to maintain with larger code. As for why the variable is reset its because it is reinitialized when re-entering the frame. All the code you placed on the timeline frame is re-run when you enter that frame again, such as stop() when placed on the first frame of a multi-frame animation will be called each time it reaches the first frame again and so it goes for initializing variables as well.

 
avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

I’m fairly new to coding…. i used to code along time ago and just started learning everything again and my game is a turn based game and the code for it is in the key frame actions… i copied it from a video and I’m not sure how I would convert everything to classes

 
avatar for NineFiveThree NineFiveThree 1370 posts
Flag Post

Read the 4 parts of “Understanding Classes in As3”

http://www.untoldentertainment.com/blog/flash-and-actionscript-911/

 
avatar for EndlessSporadic EndlessSporadic 10478 posts
Flag Post

What I did for my first cheap games was to make one layer on my timeline solely for holding variables. The frame is created on the first frame of gameplay so it is only initialized once. The layer extends for the duration of the gameplay frames. By far one of the worst ways to do it, but it works.

 
avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

Um why is that a bad way to do it? Also for the classes for as3… I’m programming in as2 not as3 so will it still work?

 
avatar for RTL_Shadow RTL_Shadow 1020 posts
Flag Post

1. Classes are MUCH more organized and speedy.
2. AS3 is 10x faster.

 
avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

I bet it is but I’m still an as2 nooby and I don’t wanna do as3 yet. So far from what I read in the understanding classes (part 1) it says all the code in one key frame. I’m wondering if I have my fighting code (which say how the turn based fights go) in the main menu frame will it still work in the key frame which has the first battle?

 
avatar for dragon_of_celts dragon_of_celts 279 posts
Flag Post

When you’re still an AS2 noobie is the best time to move to AS3. Why would you want to learn a bunch of things you’ll have to unlearn/re-learn in order to do it properly (and much better)?

 
avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

Well the main problem with that is I’m watching a tutorial on how to make a turn based game and the tutorial is only as2 not as3…

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by gulzaibthegreat1:

I go to the next frame and when I go back to the first frame the variable is again 10. Why?

Simple:

I have a key frame which states that: variable1 = 10

That code runs every time that keyframe is hit. ;)

 
avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

Haha okay well I fixed the problem, I put the variables into the first key frame so this problem is solved. I still want to convert all this into class files though… So is there a place where I can learn to convert my as2 code into as3?

 
avatar for Khronosis Khronosis 130 posts
Flag Post

You should consider buying a book. They tend to have better detail than online tutorials/videos.

Here’s an example of a good book on AS3:
http://www.amazon.com/Foundation-Game-Design-Flash-Foundations/dp/1430218215

 
avatar for BigJM BigJM 468 posts
Flag Post

You can use classes in AS2 too. AS3 is much better though.

 
avatar for Degraiver Degraiver 9 posts
Flag Post

sorry to come in a day late, but I suggest moving on to AS3 like they suggest. I coding in AS2 for a long time and didn’t want to go to AS3 because it takes away on stage coding(which I still miss) but if you learn AS2 its a bit harder to learn AS3. You have to unlearn all the habits of AS2. (RIP OnStage coding)

 
avatar for skyboy skyboy 6261 posts
Flag Post

Originally posted by gulzaibthegreat1:

Well the main problem with that is I’m watching a tutorial on how to make a turn based game and the tutorial is only as2 not as3…

there are other tutorials in AS3, including turn based. you won’t be making the exact same game, but after you learn concepts, you won’t need to copy/paste without really understanding the code, you can just write it.

 
avatar for BigJM BigJM 468 posts
Flag Post
Originally posted by Degraiver:

I coding in AS2 for a long time and didn’t want to go to AS3 because it takes away on stage coding(which I still miss)

No it doesn’t. That’s compiler-specific.

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by BigJM:
Originally posted by Degraiver:

I coding in AS2 for a long time and didn’t want to go to AS3 because it takes away on stage coding(which I still miss)

No it doesn’t. That’s compiler-specific.

 
avatar for BigJM BigJM 468 posts
Flag Post

Who wouldn’t miss that?!

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by BigJM:

Who wouldn’t miss that?!

That’s actually a current project. :P

My reason is pretty shitty, but it makes finding things much easier. Each frame corresponds to an interactivity within a video, so as the video plays it will proceed through the timeline and stop at each interactive (or on-screen text). The blank frames are when nothing needs to be on screen.

So much easier to get to things than having a thousand objects on layers in a single frame, or trying to manage it via code (“‘rectus muscle’ on the component separation screen needs to move 10 pixels to the left” would require searching through a giant class file for the code that attaches a libary object to the screen and altering the x/y values rather than me saying “well that’s section 2, so let me just…there, fixed”).

 
avatar for gulzaibthegreat1 gulzaibthegr... 291 posts
Flag Post

alright guys so im going to try and switch into as3… and ill learn about as3 and classes… and ill search up turn based games in as3 etc. etc. thanks for all the help :)

 
avatar for jscherer jscherer 41 posts
Flag Post

If you really want to learn about class structure then I would head on over to Udacity. Now you will not be learning AS3 but you will get all the fundamental concepts of class structures and the such. Some of the stuff taught in their learning programming languages may not be what you want but all the fundamentals are there and trust me on once you learn one language, it is relatively easy to learn another. Others may disagree but I find that to be true.