How can I make a symbol visible only if other two symbols are visible in ActionScript 3.0? - actionscript-3

I want to make a symbol visible (symbol3) only if symbol1 and symbol2 are visible in ActionScript 3.0 (Adobe Animate CC), but I can't make it, because I am really new in programming... Can anyone help me? I have this code:
symbol1.visible = false;
symbol2.visible = false;
symbol3.visible = false;
button1.addEventListener(MouseEvent.CLICK, fl_ClickToHide_1);
function fl_ClickToHide_1(event:MouseEvent):void
{
symbol1.visible = true;
}
button2.addEventListener(MouseEvent.CLICK, fl_ClickToHide_2);
function fl_ClickToHide_2(event:MouseEvent):void
{
symbol2.visible = true;
}
symbol3.visible = symbol2.visible && symbol1.visible
Thanks a lot,
Tom

The reason why it doesn't work for you is because symbol3.visible = symbol2.visible && symbol1.visible; is being executed right after all is hidden and buttons listeners were added. After you click buttons only code in fl_ClickToHide_1 or fl_ClickToHide_2 is being executed and in there you just change visibility of one symbol. If you want to execute checking after click you can do something like that:
symbol1.visible = false;
symbol2.visible = false;
symbol3.visible = false;
button1.addEventListener(MouseEvent.CLICK, fl_ClickToHide_1);
function fl_ClickToHide_1(event:MouseEvent):void
{
symbol1.visible = true;
do_magic();
}
button2.addEventListener(MouseEvent.CLICK, fl_ClickToHide_2);
function fl_ClickToHide_2(event:MouseEvent):void
{
symbol2.visible = true;
do_magic();
}
function do_magic():void
{
symbol3.visible = symbol2.visible && symbol1.visible;
}

Related

AS3 Custom Cursors Invisible

I've made a new question since it makes sense since the problem isn't the same anymore.
I'm making a colouring game.
CanvPark_mc is the Movieclip with the details of creating the canvas and where all the brush details are located. The Huge, Medium and SmallSelected1 variables, are variables that are changed when you click the brushes, so you're able to identify them.
I want to switch cursors depending on the brush that was last clicked. This can be done through the first if paramenters.
Right now, this is my code to switch, thanks to the help of #NBooo in the previous question
var cursor_mc: MovieClip = new MovieClip();
if (CanvPark_mc.HugeSelected1 == true) {
cursor_mc = cursor1_mc; //Big Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc; //Medium Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.SmallSelected1 == true) {
cursor_mc = cursor3_mc; //Small Cursor
removeChild(cursor_mc);
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);
function moveCursor(myEvent: MouseEvent) {
if (CanvPark_mc.SmallSelected1 == false && CanvPark_mc.MediumSelected1 == false && CanvPark_mc.HugeSelected1 == false) {
Mouse.cursor = "auto";
} else if (cursor_mc){
addChild(cursor_mc);
setChildIndex(cursor_mc, this.numChildren - 1);
cursor_mc.x = stage.mouseX;
cursor_mc.y = stage.mouseY;
cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;
Mouse.hide();
}
}
The problem with this code, unfortunately, is that, whenever I click the said buttons, the cursor disappears. It doesn't change itself to any of the MovieClips I previously made.
Note that the removeChild in the if blocks is there in hopes of removing previous instances of cursor_mc on the screen after clicking them in the past.
In tests, it only changes, in the first if blocks, one of the parameters has a = instead of ==.
Example :
if (CanvPark_mc.HugeSelected1 == true) {
cursor_mc = cursor1_mc; // Big Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc; //Medium Cursor
removeChild(cursor_mc);
}
if (CanvPark_mc.SmallSelected1 = true) {
cursor_mc = cursor3_mc; //This is the one he'll run, showing the smallest cursor
removeChild(cursor_mc);
}
In addition, if they are all reduced down to just =, the code will opt for the last if in the list.
I think there's a problem with my code and I can't tell what it is after hours of trying.
Could any of you help me figure this out?
I found the answer to my own question, so I'll be posting the code to help others out.
var cursor_mc: MovieClip = new MovieClip();
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);
function moveCursor(myEvent: MouseEvent) {
if (CanvPark_mc.HugeSelected1 == true) {
cursor_mc = cursor1_mc;
cursor1_mc.visible = true;
cursor2_mc.visible = false;
cursor3_mc.visible = false;
}
if (CanvPark_mc.MediumSelected1 == true) {
cursor_mc = cursor2_mc;
cursor1_mc.visible = false;
cursor2_mc.visible = true;
cursor3_mc.visible = false;
}
if (CanvPark_mc.SmallSelected1 == true) {
cursor_mc = cursor3_mc;
cursor1_mc.visible = false;
cursor2_mc.visible = false;
cursor3_mc.visible = true;
}
if (CanvPark_mc.SmallSelected1 == false && CanvPark_mc.MediumSelected1 == false && CanvPark_mc.HugeSelected1 == false) {
Mouse.cursor = "auto";
} else if (cursor_mc) {
addChild(cursor_mc);
setChildIndex(cursor_mc, this.numChildren - 1);
cursor_mc.x = stage.mouseX;
cursor_mc.y = stage.mouseY;
cursor_mc.mouseChildren = cursor_mc.mouseEnabled = false;
Mouse.hide();
}
}
The if block wasn't inside the function, so it wasn't correlating the code togheter. I put it inside as a test and it worked. Since when you click to change your mouse to a brush, the Movieclip starts following your mouse around. If you change to another brush, the movieclip gets left there. This is fixed by toggling it's visibility in the if's. :)
It's working as intended now.
Hope this helps someone!

AS3: Maintain Checkbox state when returning to a frame with checkboxes

So I asked a question yesterday about how to control the state of some checkboxes here: Getting Checkboxes to retain their state on return to frame
However this has caused another problem. Whilst the values of the checkboxes are retained, their relationship to the objects they represent is not.. for example, on return to the home screen the checkbox value will be filled in as true but the button that that checkbox represents won't be visible until I uncheck and recheck the box.
I've been trying to store the Boolean value of the checkbox in a variable and then re use it upon return to the screen but I just don't' understand enough of the syntax to get it work.
Looking at the code below I'm wondering whether it's because I'm setting the default state of the button visibility right at the start of the code to false? Do I need to get Area_1_Btn.visible to check for the Boolean state?
Any help greatly appreciated as I'm getting more and more frustrated at my lack of understanding heh.
import flash.events.Event;
/* Ensures that all checkboxes begin in the off state.*/
Area_1_Btn.visible = false;
Area_1_Chk.visible = true;
Area_2_Btn.visible = false;
Area_2_Chk.visible = true;
ShowAll_Chk.visible = true;
/* Defines the Show All Checkbox and sets states to true/false*/
ShowAll_Chk.addEventListener(Event.CHANGE, toggleMulti, false, 0, true);
function toggleMulti(e:Event):void
{
var SAC:Boolean = e.target.selected;
if (SAC)
{
Area_1_Chk.selected = true;
Area_1_Btn.visible = true;
Area_2_Chk.selected = true;
Area_2_Btn.visible = true;
}
else
{
Area_1_Chk.selected = false;
Area_1_Btn.visible = false;
Area_2_Chk.selected = false;
Area_2_Btn.visible = false;
}
}
/* Toggles the button state*/
function toggleArea_1_Btn(e:Event):void
{
var A1B:Boolean = e.target.selected;
if (A1B)
{
Area_1_Btn.visible = true;
}
else
{
Area_1_Btn.visible = false;
}
/* previous method for toggling button state*/
}
function toggleArea_2_Btn(e:Event):void
{
Area_2_Chk.selected ? Area_2_Btn.visible = true:Area_2_Btn.visible = false;
}
/* Listens to the state of the checkbox and switches the button on*/
Area_1_Chk.addEventListener(Event.CHANGE, toggleArea_1_Btn, false, 0, true);
Area_2_Chk.addEventListener(Event.CHANGE, toggleArea_2_Btn, false, 0, true);
/* Listens for a mouse click and then instructions function below*/
Area_1_Btn.addEventListener(MouseEvent.CLICK, A1_ClickToGoToAndStopAtFrame);
Area_2_Btn.addEventListener(MouseEvent.CLICK, A2_ClickToGoToAndStopAtFrame);
function A1_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
gotoAndStop(2);
}
function A2_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
gotoAndStop(3);
}
stop();
Here are some optimizations and additions that should do the trick:
import flash.events.Event;
import flash.events.MouseEvent;
//a method you can call to set the visibility of all checkbox related items. Pass in false to hide them all
function showCheckBoxes(val:Boolean = true):void {
Area_1_Chk.visible = val;
Area_2_Chk.visible = val;
ShowAll_Chk.visible = val;
Area_1_Btn.visible = val;
Area_2_Btn.visible = val;
if(val) setButtonVisibility(); //if showing, show/hide the buttons based off the check box value
}
//this will update the button visibility based off the checkbox selected value.
function setButtonVisibility(e:Event = null):void {
Area_1_Btn.visible = Area_1_Chk.selected;
Area_2_Btn.visible = Area_2_Chk.selected;
}
function toggleMulti(e:Event):void {
Area_1_Chk.selected = ShowAll_Chk.selected;
Area_2_Chk.selected = ShowAll_Chk.selected;
setButtonVisibility();
}
function buttonClick(e:Event):void {
showCheckBoxes(false); //hide everything since we're moving to a new frame
switch(e.currentTarget) { //e.currentTarget is the button that was clicked
case Area_1_Btn:
gotoAndStop(2);
break;
case Area_1_Btn:
gotoAndStop(3);
break;
}
}
//add all the listeners
ShowAll_Chk.addEventListener(Event.CHANGE, toggleMulti, false, 0, true);
Area_1_Chk.addEventListener(Event.CHANGE, setButtonVisibility, false, 0, true);
Area_2_Chk.addEventListener(Event.CHANGE, setButtonVisibility, false, 0, true);
Area_1_Btn.addEventListener(MouseEvent.CLICK, buttonClick);
Area_2_Btn.addEventListener(MouseEvent.CLICK, buttonClick);
showCheckBoxes(); //call this right now so the visibility updates when the frame loads.
stop();

how to disable movieclip?

I am using Adobe Flash CS5, Action Script 3.0. I want to disable my movie clip. How to do that?
My code:
var index:Array = ([0,1,2,3]);
var radioGroup1:RadioButtonGroup = new RadioButtonGroup("question1");
rb1.group = radioGroup1;
rb2.group = radioGroup1;
rb3.group = radioGroup1;
rb4.group = radioGroup1;
b1.addEventListener(MouseEvent.CLICK, submitclick,false);
function submitclick(event:MouseEvent):void
{
if (radioGroup1.selectedData == null)
{
b1.mouseEnabled = false;
//ExternalInterface.call("Test","Please selecte one answer");
//return;
} else {
if (radioGroup1.getRadioButtonIndex(radioGroup1.selection) == 2)
{
myscore += 10;
}
}
if (compquest>=maxquest)
{
gotoAndStop(1,"Scene 6");
}
else
{
MovieClip(this.root).gotoAndStop(1, "Scene " + questions[compquest++]);
}
}
I want to disable this b1 movieclip. So anyone could tell me what am I doing wrong?
If, by disable, you mean to make it unclickable and faded like a disabled button. Just reduce the alpha a bit and set its mouseEnabled property to false.
function submitClick(event:MouseEvent){
b1.alpha = .6;
b1.mouseEnabled = false;
}
This way the user will not be able to click it and it appear disabled.
First you could write, because event.target is p1:
function submitclick(event:MouseEvent):void
{
event.target.mouseEnabled = false;
}
On the other hand, the condition you want to evaluate in your if statement is probably false:
radioGroup1.selectedData == null
Therefore the associated code isn't executed:
b1.mouseEnabled = false;
You should test in your function and before your if statement:
trace(radioGroup1.selectedData == null);

Customizing Clothes on Character in Game User Personalization

I could use help with customization in Flash games! I am pretty new to AS3 and have a game I am building where the user can dress the character based on a few options and a color picker, then move on to a race. I cannot get the clothes that are chosen to stay, and the ones excluded leave, without all of them staying or leaving. I've tried variables, if/else conditions and switch statements, but nothing is working. I have a feeling it's a condition, but I don't know how to write it and can't find anyone in a similar boat.
I have been scouring my books (Flash CS6 Missing Manual and ActionScript 3.0 Cookbook), and I've gotten very close, but nothing works. I really could use a lot of help with this, it's a final project and the stress might be hiding the answer, but I surely don't have it.
Not sure if I did this right, I've never used this site before. Thank you in advance!
UPDATE
Here is a link to where the .swf file is currently uploaded for your input.
http://yellownotebook.weebly.com/other-work.html
Please ignore the DONE button at the beginning, I am working on that still. I need the clothes to be invisible on the start up, and the remaining unselected clothes after that to be discarded? Invisible? I still don't understand which way is best for this situation. I started reading something about Display Lists?
I'm also not sure what part of the code would be most helpful, so here it is.
var mcdress2 = mcdress2
var mcpants = mcpants
var mcshirt = mcshirt
var mctop = mctop
var clothes = mctop + mcpants + mcshirt + mcdress2
var fairy = clothes + mcwings + mcfay
mcfay.visible = false;
mctop.visible = false;
mcshirt.visible = false;
mcpants.visible = false;
mcdress2.visible = false;
mcwings.visible = false;
cpClothes.visible = false;
import fl.events.ColorPickerEvent;
fairybg.btnplay.addEventListener(MouseEvent.CLICK,clickPlayListener);
btndone.addEventListener(MouseEvent.CLICK,clickDoneListener);
fairybg.gotoAndStop("Game Start");
function clickPlayListener(evt:MouseEvent):void
{
fairybg.gotoAndStop("Background Start")
mcfay.visible = true;
mctop.visible = true;
mcshirt.visible = true;
mcpants.visible = true;
mcdress2.visible = true;
mcwings.visible = true;
cpClothes.visible = true;
}
//fairy.scaleY = fairy.scaleX
function clickDoneListener(evt:MouseEvent):void
{
fairybg.gotoAndStop("Background Fly")
fairybg.gotoAndStop("Background Fly")
//fairy.width = 1/2
//fairy.height = 1/2;
//if it was this way, she would be bare when she flies, needs "if" condition?
//mctop.visible = false;
//mcshirt.visible = false;
//mcpants.visible = false;
//mcdress2.visible = false;
cpClothes.visible = false;
btndone.visible = false;
}
//fairy.scaleY = fairy.scaleX
mcdress2.gotoAndStop(1);
mcdress2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
function mouseDownListener (event:MouseEvent):void
{
mcdress2.gotoAndStop("Dress End");
mctop.gotoAndStop("Top Start");
mcpants.gotoAndStop("Pants Start");
mcshirt.gotoAndStop("Shirt Start");
}
mcshirt.gotoAndStop(1);
mcshirt.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener2);
function mouseDownListener2 (event:MouseEvent):void
{
mcshirt.gotoAndStop("Shirt End");
mcdress2.gotoAndStop("Dress Start");
mctop.gotoAndStop("Top Start");
}
mcpants.gotoAndStop(1);
mcpants.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener3);
function mouseDownListener3 (event:MouseEvent):void
{
mcpants.gotoAndStop("Pants End");
mcdress2.gotoAndStop("Dress Start");
}
mctop.gotoAndStop(1);
mctop.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener4);
function mouseDownListener4 (event:MouseEvent):void
{
mctop.gotoAndStop("Top End");
mcshirt.gotoAndStop("Shirt Start");
mcdress2.gotoAndStop("Dress Start");
}
//use color picker to change clothing color (all items same color)
cpClothes.addEventListener(ColorPickerEvent.CHANGE,changeColorPicker);
function changeColorPicker(evt:ColorPickerEvent):void
{
var myColorTransform = new ColorTransform ();
myColorTransform.color = evt.color;
mcdress2.transform.colorTransform = myColorTransform;
mctop.transform.colorTransform = myColorTransform;
mcshirt.transform.colorTransform = myColorTransform;
mcpants.transform.colorTransform = myColorTransform;
//trace ("color changed")
//trace (evt.color)
//trace (mcdress.color)
//opaqueBackground = evt.color;
}
cpClothes.colors =
[0xF7977A,0xFFF79A,0x6ECFF6,0xF49AC2];
switch (clothes) {
case "Dress End" :
mcshirt.visible = false;
mctop.visible = false;
mcpants.visible = false;
break;
case "Top End" :
mcshirt.visible = false;
mcdress2.visible = false;
mcpants.visible = false;
break;
case "Shirt End" :
mctop.visible = false;
mcdress2.visible = false;
mcpants.visible = false;
break;
default :
//mcdress2.gotoAndStop("Dress Start")
//mctop.gotoAndStop("Top Start")
//mcshirt.gotoAndStop("Shirt Start")
//mcpants.gotoAndStop("Pants Start")
mcdress2.visible = true;
mctop.visible = true;
mcshirt.visible = true;
mcpants.visible = true;
}
/*if (clickDoneListener==true) {
fairy.width = .5
fairy.height = .5;
}*/
//trace ("it works!")
//var _mcpants:mcpants;
//function newmcpants(e:MouseEvent):void
//{
//if (_mcpants)
// return
//_mcpants = new mcpants();
//_mcpants.x = 263.35;
//_mcpants.y = 270.40;
//addChild(_mcpants);
//}
//function deletemcpants(e:MouseEvent):void;
//{
//if (_mcpants && contains(_mcpants))
//removeChild(_mcpants);
//_mcpants = null;
//displayText("Deleted mcpants successfully!");
//}
As you can see, I have a lot of code commented out, I have been trying everything I can find for it to work. I also am trying to scale her down 50% after the DONE button is clicked and the game is started, but I have not figured out a way for that one either. Thank you so so much for any help!
You must add information to your system about which clothes have been selected. You are right that it is about conditionals but your conditional will need the information to base the selection on. I think there are 3 alternatives:
Manage a collection of the clothes that are put on
Make clothes objects that have a property, cloth.weared = true or false
Have slots for your clothes: upperbody, lowerbody etc. and assign the selected clothes there: upperbody = somecloth;
I made 1. as a bare bones example for you. Hopefully you can understand it! May not be the most elegant solution but does work, is quite simple and I think the logic is quite close to what you already have: http://jsfiddle.net/n37qx/
var selected = [];
var SHIRT = "shirt";
var DRESS = "dress";
var TROUSERS = "trousers";
var all = [SHIRT, DRESS, TROUSERS];
//logic
function putOn(cloth) {
if (arraycontains(selected, cloth)) {
return;
}
//replace check
if (cloth === SHIRT) {
removeIfContains(selected, DRESS);
} else if (cloth === DRESS) {
removeIfContains(selected, SHIRT);
removeIfContains(selected, TROUSERS);
}
selected.push(cloth);
}
function hideNotSelected() {
for (var i=0; i < all.length; i++) {
var cloth = all[i];
if (arraycontains(selected, cloth)) {
log("wearing - not hiding: " + cloth)
} else {
log("hide: " + cloth);
}
}
}
//test flow - code instead of UI actions
log("1. Put on shirt + trousers, dress should get hidden:");
putOn(SHIRT)
putOn(TROUSERS);
hideNotSelected();
log("---");
log("2. Put on dress - trousers and shirt should get hidden:");
putOn(DRESS);
hideNotSelected();
//utilities
...
That is Javascript. Actionscript is basically Javascript (ECMAScript) with extensions. You have nice array helpers there already so don't need the ones I added to the bottom there. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/ArrayList.html

Actionscript 3 detect successive key press

I have 2 movie clips. One has a character walking and the other one he's running. How do I make it so that if I press the left arrow key, it will play the walking clip. If I press the left arrow in quick succession, it will switch to the running clip. I am using Actionscript 3.
This requires a bit of codework as Flash AS3 does not have native support for double clicks. Here is a simple example of how you can achieve this effect with SetTimeout
var keyCodeToListenFor = '13'; // can be any keycode;
var keySinglePress:Boolean = false;
var keyDoublePress:Boolean = false;
var timeToWaitForDoublePress:Number = 400;
var waitingForDoublePress:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
function handleKeyDown(e:Event) {
if (e.keyCode == keyCodeToListenFor) {
keySinglePress = true;
if (waitingForDoublePress) {
keyDoublePress = true;
}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress) {
trace('You logged a double press!')
} else if (keySinglePress) {
trace('You logged a single press!');
}
}