...

View Full Version : 3 Action Script problems I'm having!



Mayron
09-07-2012, 09:03 PM
Hi guys. I'm a beginner at Action Script 3 and am trying to make a very simple game but having 3 issues I need to overcome and was wondering if you could help me out.

1st. I want to create a button in Flash. When I press on it and release, it changes colour to green or red depending on whether or not it was the correct button you were meant to click (the boxes are the answers to a question). However I cant even get it to turn one colour as on the button time line, there are 4 buttons and the 4th is called Hit. I turned the hit key frame to green but it never comes up on release. Any ideas how to do this?

2nd: There are going to be 3 rows of boxes linked to one quiz question. If you get 0 questions right, go to this frame when you press the next button, if you get 1 right, go to frame b, 2 right, frame c, and 3 right, go to frame c. How do I keep note of each out come and how do I make it so once you select one of 3 boxes from each row, you cannot select any from that row again?

and finally 3rd: I have a movie clip animation of a health bar. I want to have it remain constant throughout the animation so that for each out come, it will increase in frames on its own time line (not the main one) by +10 frames to give the effect of the player/enemy loosing health. The only problem is that the buttons and key frames jump around the animation and I do not want the value of which frames the health bar is on to change. Not sure how on earth this would be possible.

If this makes any sense at all and you have some code that you think I could use to help me out then I would be very grateful.

I've tried explaining as best as I can but it is hard to explain my issues with just text so if you have any questions, let me know and I will go through them :)

Thank you for reading!

waxdoc
09-07-2012, 10:55 PM
Have not figured out all of yours query, but want to correct big error about button HIT.

The classic Flash button has four states: UP OVER DOWN HIT.
The HIT just represents the clickable area for the button. You can create a "hidden button" by eliminating any visuals of the first 3 states (UP OVER DOWN) and just define the clickable area in the HIT state.

You can create buttons from Movie Clips, but would still have representation of the 4 above states.

Movie Clip symbols do have their own independent timelines, which can occupy one frame and will run to the end of its timeline (unless stopped). A Graphics symbol's timeline is dependent upon the root movie it is placed in; if the parent timeline is shorter than the Graphic symbol's, it will be truncated. So wonder if using a 'Graphics symbol (or just a plain static image) would solve some of your questions.

See also http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html


The Sprite class is a basic display list building block: a display list node that can display graphics and can also contain children.

A Sprite object is similar to a movie clip, but does not have a timeline. Sprite is an appropriate base class for objects that do not require timelines. For example, Sprite would be a logical base class for user interface (UI) components that typically do not use the timeline.

The Sprite class is new in ActionScript 3.0. It provides an alternative to the functionality of the MovieClip class, which retains all the functionality of previous ActionScript releases to provide backward compatibility.

djpaul1963
09-08-2012, 11:21 AM
About step 1, the solution you need is to forget about button and hit states and replace with a movie clip that you treat like a button. This way, the movie clip will have the frames you want, each one with a different color. Every time a user clicks right, you move the movieclip playhead to the correct frame.
For example, you will have a white square on frame 1, a green on frame 2 and a red on frame 3. you name this movieclip as anything you like (answer_button for eaxmple) and then from your actionscript you do:

answer_button.buttonMode = true; // this needed to change the mouse cursor to pointer when the mouse hovers the answer_button
answer_button.addEventListener(MouseEvent.CLICK, onAnswerClick);

protected function onAnswerClick(e:MouseEvent):void
{
// here you implement your game logic like:
if (answer == "correct") e.currentTarget.gotoAndStop(2); // the 'button' shows green
else e.currentTarget.gotoAndStop(3); // the 'button' shows red
}

About step 2, you need a global int variable (I hope you know what global variables are) to keep the score. Also, you need an array of Booleans to keep the questions status. If the user selected question 2 for example, you set true that Boolean and deny further interaction.

About step 3, not sure I understood what exactly you mean, but if I got it right:

the health movie clip is an animation of lets say 100 frames, changing a gradient form green to red. You add a second layer to that movieclip with an action keyframe on frame 1 where you write stop(); You name this as health_bar or whatever. When you place that on the main timeline you see only one frame (because the 100 frames are the animation of that particular movie clip, they are INSIDE the movie clip). So, even if you extend the frames of that layer to have the movie clip active till the end of the timeline, the health_bar will be just a single frame. Now, each time you want the health_bar to show a different life status you do a health_bar.gotoAndStop(XX) where XX is the frame you want.

I hope I helped. If you need more help upload your .fla and .as file to show you in practice.

Mayron
09-08-2012, 03:11 PM
Thank you both and especially thank you djpaul1963. This is very useful info to me and I'm sure I can solve my issues and yes I know what a global variable is! :D

I will give this a go now and see what I come up with :)

Edit: I tried the first code you submitted for the first step solution but I got this error:

The protected attribute can only be used on class property definitions

Mayron
09-08-2012, 03:31 PM
I may upload my files like you mentioned If I get too suck.


Got a question about the first code. Where it says "if (answer == "correct") " how would I go about defining what is the correct answer as this does not make much sense to me. Sorry that my AS3 knowledge is so bad.

I also ask this because I get the following error:
Access of undefined property answer.

Might be worth finishing a few things and uploading my files.

djpaul1963
09-08-2012, 03:57 PM
You must go some steps back and decide about the logic of your game.
When I typed "answer == correct" I tried to give you an example idea about the button color change.
Also, I assume (by the protection error you pasted) that you don't have a document class defined in your flash but you are trying to code everything in frame 1? This will give you a lot of trouble because your movie will have more than one frame I guess.
You must start from basics and go a step at a time.
Forget about the correct answers and the health bar for now and try to make the coloured button/movie clip for a start.
When you have it ready and fully working as you need, proceed further.

Mayron
09-08-2012, 05:19 PM
Thank you for the advice. I'm trying to get a script to work so that when I press one of the 3 answers, I can never press any of them again. So far I have this:


answerOne1.buttonMode = true; // this needed to change the mouse cursor to pointer when the mouse hovers the answer_button
answerOne1.addEventListener(MouseEvent.CLICK, onClickOne);
function onClickOne(e:MouseEvent)
{
answerOne1.gotoAndStop(2)
<"answerOne2s mouseover event becomes void">
}

answerOne2.buttonMode = true; // this needed to change the mouse cursor to pointer when the mouse hovers the answer_button
answerOne2.addEventListener(MouseEvent.CLICK, onClickTwo);
function onClickTwo(e:MouseEvent)
{
answerOne2.gotoAndStop(2)
}


As you can see I need something here:
<"answerOne2s mouseover event becomes void">

So when I press on button 1, I cannot use button 2.

Not sure what though :(

djpaul1963
09-08-2012, 08:20 PM
No need to make it so complicated, sometimes the simpler is better.

var global_boolean:Bolean;

button1, button2... buttonN.addEventListener(MouseEvent.CLICK, onButtonClick);

function onButtonClick(e:MouseEvent):void
{
if (global_boolean) return;
global_boolean = true;
// bla bla bla do whatever you want bla bla bla //
}

Need explanation?

Mayron
09-09-2012, 10:13 AM
No that makes perfect sense. Thank you!

Mayron
09-09-2012, 06:28 PM
I think I've made a complete mess with this lol.

This is my Code:

var global_boolean:Boolean;
answerOne1, answerOne2, answerOne3.addEventListener(MouseEvent.CLICK, saved);

function saved(e:MouseEvent):void
{
if (global_boolean) return;
global_boolean = true;

answerOne1.buttonMode = true;
answerOne1.addEventListener(MouseEvent.CLICK, onClickOne);

answerOne2.buttonMode = true;
answerOne2.addEventListener(MouseEvent.CLICK, onClickTwo);

answerOne3.buttonMode = true;
answerOne3.addEventListener(MouseEvent.CLICK, onClickThree);

function onClickOne(e:MouseEvent)
{
answerOne1.gotoAndStop(3)
}
function onClickTwo(e:MouseEvent)
{
answerOne2.gotoAndStop(2)
}

function onClickThree(e:MouseEvent)
{
answerOne3.gotoAndStop(3)
}
}


I do not get any errors but what I want is not working. I want to be able to press one of those 3 buttons (answerOne1, answerOne2, answerOne3) and after, you cannot press any of them again.

I do not understand what I've done wrong. I know I'm bad.

djpaul1963
09-09-2012, 07:23 PM
It's totally wrong because you miss basic actionscript skills I am afraid.
I think you need to read an actionscript for beginners book before going deeper and try what you try now.
I am including here the code you need with a bit of explanation:

var flag:Boolean;

answerOne1.buttonMode = true;
asnwerOne1.name = "one"; // you name each movie clip with a unique instance name so it will be easy to use a single function for all of your buttons
answerOne1.addEventListener(MouseEvent.CLICK, onAnswer);

answerOne2.buttonMode = true;
asnwerOne2.name = "two";
answerOne2.addEventListener(MouseEvent.CLICK, onAnswer);

answerOne3.buttonMode = true;
asnwerOne3.name = "three";
answerOne3.addEventListener(MouseEvent.CLICK, onAnswer);

function onAnswer(e:MouseEvent):void
{
if (flag) return; // some button got pressed before so no action is allowed
flag = true; // first time here so we raise the flag to disallow further actions
if (e.currentTarget.name == "two") e.currentTarget.gotoAndStop(2); // the correct button is that with name 'two' so we move to the green color frame. if you are curious about the e.currentTarget, try to debug your movie. You will find that the event holds the object that got clicked
else e.currentTarget.gotoAndStop(3); // all other buttons are wrong so we move to the red frame
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum