AS3 making an array invisible!? how? - actionscript-3

var sunflowers30:Array = [sunflowerpetal1,sunflowerpetal2,sunflowerpetal3,sunflowerpetal4,sunflowerpetal5,sunflowerpetal6];
sunflowers30.visible = false;
Why is the code up there not working?? what am i doing wrong?? ( trying to make the array invisible).
also should this code not work as well? (below)( trying to go to a different scene once array ( all instances) are hidden/invisible).
if(sunflowers30.visible == false)
{
gotoAndPlay(1, "theplace")
}
;
Sunflowerpetal 1-6 are instances that are on my stage currently
Sunflowers30 is the array i made from the instances on stage.
"Theplace" is the next scene
Help and comments are much appreciated I'm kind of new to AS3 and code in general but i bet you code gurus could help me out, many thanks ahead of time!

Array does not have a visible property.
What you need to do is loop through the array and set the property on each element of that array.
var sunflowers30:Array = [sunflowerpetal1,sunflowerpetal2,sunflowerpetal3,sunflowerpetal4,sunflowerpetal5,sunflowerpetal6];
for each( var obj:Object in sunflowers30 ){
obj.visible = false;
}
// or another way or doing it
for( var i:int = 0; i<sunflowers30.length; i++){
obj.visible = false;
}
And as your second question asks if it should work the answer is no.
You are again targeting the array and not the object you want to test if it is visible.
if(sunflowerpetal1.visible == false)
{
gotoAndPlay(1, "theplace")
}
;

Related

how do I call a function if the image clicked is equal to its matching word? Actionscript3

When my random word appears the user has to memorise it and click the correct corresponding image to it. I'm trying to write the code that runs if the user selects the right image. I have paired my words and images in my array. I'm just unsure as how to go about calling this function.
This is what I've attempted so far, but this isn't working. I'm new to actionscript3 so excuse the lack of knowledge as I am trying to teach myself.
All help greatly appreciated!!
This is one way you can do this:
See code comments
basket.visible = false;
//------ Home Button ------\\
backhome1btn.addEventListener(MouseEvent.CLICK, goback1Click);
function goback1Click(event:MouseEvent):void{
gotoAndStop("homepage");
}
//-------------------
var score:int = 0;
var items:Array = new Array(); //store all food items in array
var wordsToShow:Array = new Array(); //store all words to show in array - this is the array that will keep track of which has been asked (or rather not asked yet)
//to reduce redundant code, call this with each food item (below)
function initFoodItem(item:MovieClip, word:String):void {
item.word = word; //forget the array, just store the word on the image as a dynamic property
item.addEventListener(MouseEvent.CLICK, foodClicked);
items.push(item); //add to array
wordsToShow.push(item); //add to array
item.visible = false;
}
initFoodItem(oc, "Orange Juice");
initFoodItem(sand, "Sandwich");
//...repeat for all other food items
//now randmize the words to show array:
wordsToShow.sort(function(a,b):int {
return(Math.random() > .5) ? 1 : -1;
});
var curAnswer:MovieClip; //a var to store the current correct answer
//this does the next question per se
function displayWord():void {
if(wordsToShow.length < 1){
//they've all been asked
gotoAndPlay("gameoverpage");
return;
}
curAnswer = wordsToShow.pop(); //assigns the last item in the array to the cur asnwer, and pop also removes that item from the array (so it won't get asked again)
randomword.text = curAnswer.word; //assign the text to the word value of this item
randomword.visible = true;
remember.visible = true;
}
remember.addEventListener(MouseEvent.CLICK, readyClick);
//when you click your ready button
function readyClick(e:MouseEvent):void {
//we have an array of all items, let's loop through it and change them all to be visible
for (var i:int = 0; i < items.length; i++) {
//items[i].alpha = 1; //alpha values are 0 - 1 in AS3
items[i].visible = true; //use visible instead of alpha if just toggling visibility, it's more efficient
}
randomword.visible = false;
remember.visible = false;
bask.visible = true;
notepape.visible = false;
//! another reason to use visible instead of alpha = 0, is alpha = 0 items will still be clickable! visible = false items cannot be clicked.
}
function foodClicked(e:MouseEvent):void {
if (e.currentTarget == curAnswer) {
//if the current target (the item clicked) is the same item as what we stored in curAnswer, you answered correctly, so do this:
score += 10; //or however much you get for answering correctly
gotoAndStop("listpage"); //not sure what this does?
displayWord();
}else {
//wrong
gotoAndStop("gameoverpage");
}
}

AS3 - Using a For Loop to Update Multiple Points and Their Values in an Array

I'm a bit new with AS3 (but not really with coding) so please forgive my ignorance. I'm creating a small function that will be called by a Main Function to update the position of 52 Pointers that have the x and y position of multiple point objects (empty movie clips). It will also then update two global arrays with those values (one array for the x and one for the y).
The problem is, as there are 52 of them, and they will probably grow in quantity, I'd like to be able to use a FOR function to do it, but I can't seem to be able to figure it out.
I get this error: Access of undefined property _point.
Here is a piece of the code that dream about:
function happyFunc():void
{
var avpointers:int = 52;
var vpointx:Array = new Array();
var vpointy:Array = new Array();
for (aa=0; aa<vpointers; aa++)
{
vpointx[aa] = _point[aa].x;
vpointy[aa] = _point[aa].y;
}
}
And this is the code that I'm stuck with...
function reallySadFunc():void
{
_point1 = localToGlobal(new Point(point1.x,point1.y));
//...
_point52 = localToGlobal(new Point(point52.x,point1.y));
vpointx[0] = _point1.x;
vpointx[1] = _point2.x;
//...
//oh god there are 104 lines of this why do I have to suffer
}
Thank you!
If I read your question correctly, I think this is what you need:
public static const CLIP_COUNT:int = 52;
// ...
private function happyFunc(parentClip:DisplayObjectContainer):void
{
var vpointx:Vector.<Number> = new Vector.<Number>(CLIP_COUNT, true);
var vpointy:Vector.<Number> = new Vector.<Number>(CLIP_COUNT, true);
var clipPoint:Point = new Point ();
var globalPoint:Point;
for (var i:int = 0; i < CLIP_COUNT; i++)
{
var childClip:DisplayObject = parentClip.getChildByName("point" +
(i + 1).toString());
clipPoint.x = childClip.x;
clipPoint.y = childClip.y;
globalPoint = parentClip.localToGlobal(clipPoint);
vpointx[i] = globalPoint.x;
vpointy[i] = globalPoint.y;
}
// do something with vpointx and vpointy - they're local variables
// and will go out of scope unless you declare them as class members
// or somehow return them from this function.
}
This function works by taking the parent display object (the one that contains the 52 movie clips - this could be the Stage) and iterates through them by getting each movie clip by name. The code assumes that your movie clips are called point1, point2, ..., point52.
To speed up the local-to-global coordinate conversion, two Point objects are created and then reused during the for loop to avoid creating many temporary Point instances.
Instead of using Array, use Vector.<Number> - this class has better performance than Array does.

Creating a vector array of movie clips AS3

I have several movie clips on the stage of my main .fla named btn1-btn7 which will act as buttons. I have a class file named Functions.as where an event listener is created when a button is clicked. onButtonClicked is just going to a frame on the timeline.
obj.addEventListener(MouseEvent.CLICK, onButtonClicked);
I would like the ability to set the buttonMode, visibility, etc. of all of the buttons simultaneously. I have been looking into this for a few hours and am not able to find any solutions. I am now looking into adding them to a vector (which is a new concept for me), but I am not sure how to go about executing this properly. This is what I have so far.
public var buttons:Vector.<MovieClip > = new Vector.<MovieClip > ();
function addButtons()
{
buttons.push(btn1,btn2,btn3,btn4,btn5,btn6,btn7);
for (var i:int; i<buttons.length; i++)
{
trace(buttons[i].name);
}
}
How would I go about, for example, adding the event listener to all of the objects? I will also be setting the buttonMode to true, and making them all invisible simultaneously. I don't even know if it's possible to accomplish this. Thank you in advance for any suggestions.
I'm going to asume that you use timeline code, and have instances of the buttons already placed on the stage. So, first, create the vector:
var _btns:Vector.<MovieClip> = new Vector.<MovieClip>;
_btns.push(btn1,btn2,btn43....) //add all the buttons
Than, you can init the properties of all the buttons:
var _mc:MovieClip;//helper var
for(var i:int=0,i<_btns.length;i++)
{
_mc = _btns[i];
_mc.visible = false;
_mc.buttonMode = true;
_mc.addEventListener(MouseEvent.CLICK, onClick);
}
Then, the event handler:
function onClick(e:MouseEvent):void
{
for(var i:int=0,i<_btns.length;i++)//reset all the buttons
{
_btns[i].visible = false;
}
_mc = MovieClip(e.eventTarget);
_mc.visible = true; //make visible the clicked one
}
You just need to do what you are doing with the .name property in your example code. You need to loop thru every single button in your array (or vector, if you prefer). Here is an example how to set the property of buttonMode:
function setButtonMode(b:Boolean):void {
for(var i:int=0; i<buttons.length; i++) {
var btn:MovieClip = buttons[i]; //store the current reference in a var for faster access
btn.buttonMode = b;
btn.mouseChildren = !b;
}
}

How to change my code to target a list of display objects?

if( listnumber != "listBox1")
this[listnumber].visible = false;
else
this[listnumber].visible = true;
I would like to change this statement to make the new listnumber visible, and make any other invisible.
Thanks to Baris Usakli for suggesting the mentioned code, my question just needed to be clearer
function makeVisible(a:Array,s:String):void{
for (var i:int = 0; i < a.length; i++)
{
if(a[i].name == s)
a[i].visible = true;
else
a[i].visible = false;
}
}
Your question is confusing so I've done the best I can to decipher what you want done. You can call that function from your script passing the array of objects and the string you want tested based on a variable (change the variable "name" to whichever is required) through the parameter. This won't work if there are objects with the same name (or at least it'll turn all objects with that name visible and all others invisible).
Hope it helps.

AS3 Getting which movieclip clicked on

So, I made a for loop to get several buttons in my project. It's a questionary, and I need to have a button to quickly select and navigate to any question there. I could do them all manually, but not only would my code be long and confusing, but also there are problems since there isn't always the same number of questions.
So right now I have:
function SetQuestionSquares():void{
for(var i:Number = 1; i <= TestProperties.QuestionLimit;i++){
var QuestionSquare:questionsquare = new questionsquare;
QuestionSquare.buttonMode = true;
QuestionSquare.mouseChildren = false;
QuestionSquare.x = NavLeft.x + (20 * i);
QuestionSquare.y = NavLeft.y;
QuestionSquare.questionsquaretext.text = i.toString();
addChild(QuestionSquare);
QuestionSquare.addEventListener(MouseEvent.CLICK, GoToQuestionNumber);
}
addChild(NavLeft);
addChild(NavRight);
}
function GoToQuestionNumber(e:MouseEvent):void{
WhichQuestion = ???; //I don't know what goes here.
UpdateQuestions();
trace("testing"); //Gets called correctly, so its working.
}
My problem is identifying which square was clicked. I need to have some way to grab the "e" (clicked) event, so I know which button the user clicked on.
You need .target property of the Event object:
WhichQuestion = e.target as questionsquare;
function GoToQuestionNumber(e:MouseEvent):void{
var WhichQuestion:DisplayObject = e.currentTarget as DisplayObject;
UpdateQuestions();
trace("testing");
}