ActionScript How to access child Sprites - actionscript-3

I have the following problem. Would appreciate if someone takes the pains to answer it.
Problem Description: I have a top level Sprite "TopLevel",
there is a Sprite under it "allRoutes",
there is child Sprite under it "Routes" and
there is another child under it "Route".
I want to access each level in a loop. I have tried doing it in ActionScript but not with success.
Here is the probable code (it is not compiled successfully):
package
{
import flash.display.Sprite;
public class TopLevel extends Sprite {
public function TopLevel() {
var allRoutes:Sprite = new Sprite();
for (i = 0; i < 3; i++) {
var routes:Sprite = new Sprite();
for (j = 0; j < 4; j++) {
var route:Sprite = new Sprite();
route.name = "Route:" + i + ", " j;
routes.addChild(route);
}
routes.name = "Routes:" + i;
allRoutes.name = "AllRoutes";
allRoutes.addChild(routes);
}
addChild(allRoutes);
trace (allRoutes.name);
for (i = 0; i < allRoutes.numChildren; i++) {
trace(allRoutes.getChildAt(i).name);
for (j = 0; j < allRoutes.getChildAt(i).numChildren; j++) {
trace(allRoutes.getChildAt(i).getChildAt(j).name);
}
}
}
}
}
I am expecting the following result:
AllRoutes
Routes-1
Route-1, 1
Route-1, 2
Route-1, 3
Route-1, 4
Routes-2
Route-2, 1
Route-2, 2
Route-2, 3
Route-2, 4
so on..
Hope you get the picture.
Thank you very much in advance.
thanks and regards
rr23850

Globally your code is fine, except some little things :
You forgot to declare i and j variables :
var i:int, j:int;
You forgot the concatenation operator before j in this line :
route.name = "Route:" + i + ", " + j;
If your are compiling with the strict mode, these instructions will fire errors :
allRoutes.getChildAt(i).numChildren
and
allRoutes.getChildAt(i).getChildAt(j).name
which can be in that case :
for (j = 0; j < Sprite(allRoutes.getChildAt(i)).numChildren; j++)
{
trace(Sprite(allRoutes.getChildAt(i)).getChildAt(j).name);
}
...
And as your for loops are starting by 0, you will get something like this :
AllRoutes
Routes:0
Route:0, 0
Route:0, 1
Route:0, 2
Route:0, 3
Routes:1
Route:1, 0
Route:1, 1
...
Hope that can help.

Related

adding X amount of addChild per maxvalue in loop

The assignment is the spawn several light and dark feathers according to score points from a quiz. The light feathers symbolize the correct points (light_feather), and the dark feather are the incorrect points (dark_feather) (Each are being tracked). All the feathers are supposed to line up on one line, meaning first light feathers, followed by the dark feathers. I got the quiz dynamics figured out, and the function I have posted here is only for when they press end quiz.
var light_feather:LightFeather = new LightFeather();
var dark_feather:DarkFeather = new DarkFeather();
var good_answers:uint = 0;
var bad_answers:uint = 0;
function avsluttFunc (evt:MouseEvent)
{
var sum_LightFeatherX:Number = 0;
for (var i = 0; i < good_answers; i++) {
addChild(light_feather);
light_feather.x += 12 + (i*16);
light_feather.y = 0;
trace("Lys X-verdi: " + light_feather.x);
sum_LightFeatherX += Number(light_feather.x);
return sum_LightFeatherX;
}
trace(sum_LightFeatherX);
dark_feather.x += sum_LightFeatherX;
for (var j = 1; j <= bad_answers; j++) {
addChild(dark_feather);
dark_feather.x += 12 + (j*16);
dark_feather.y = 0;
trace("Mørk X-verdi: " + dark_feather.x);
}
/*
//Resetter poengsummen
good_answers = 0;
bad_answers = 0;
*/
}
You can do what you are looking for using only one for loop, take a look :
var good_answers:uint = 2;
var bad_answers:uint = 4;
function avsluttFunc(evt:MouseEvent)
{
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
feather.x += 12 + i * (feather.width + 1);
feather.y = 0;
addChild(feather);
}
}
This code example will create 4 DarkFeather instances next to 2 LightFeather ones.
Edit :
How to add your objects to an array ?
// feathers array should be accessible for both codes (adding and removing objects)
var feathers:Array = [];
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
addChild(feather);
feathers.push(feather);
}
then to remove them from the stage, you can do for example :
for (var i:int = 0; i < feathers.length; i++) {
var feather:DisplayObject = DisplayObject(feathers[i]);
feather.parent.removeChild(feather);
}
Hope that can help.

Flex- Action Script programming Language

I am new to Action Script programming Language, and I want to create the following number pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
How can I code this pattern in Action Script Language?
I'm surprised anyone's starting to learn ActionScript (assuming you're using it for Flash) when the rest of the planet is moving toward HTML5 but, to each their own :-)
The algorithm you want for that sequence is relatively simple, and should be simple to convert into any procedural language:
for limit = 5 to 1 inclusive, stepping by -1:
for number = 1 to limit inclusive, stepping by 1:
output number
output newline
That's basically it. Based on my (very) limited exposure to AS3, that would come out as something like:
for (var lim:Number = 5; lim > 0, lim--) {
for (var num:Number = 1; num <= lim, num++) {
doSomethingWith(num);
}
}
var x:int = 5;
while(x > 0) {
var output:String = "";
for(var i:int = x; i > 0; i--) {
output = i + " " + output;
x--;
}
trace(output);
}
//Here the variable to build the string to "print"
var str:String="";
//We need two loops
//The first to change the row
for (var lim:int= 5; lim > 0; lim--) {
//Reset the string each new row
str="";
//And here the scond to build the string to "print"
for (var num:int= 1; num <= lim; num++) {
str+=num+" ";
}
//Here "print" the string
trace(str);
}
This is my code to create a pattern like that:
for (var i:int = 5; i >= 1; i--) { // represent the row
trace("1"); // print first element
for (var j:int = 2; j <= i; j++) { // represet the line
trace(" " + j.toString()); // print space and a number
}
trace("\n"); //next line
}

How to make a variable loop in actionscript 3

I'm trying to make a loop that won't crash my flash application. I want variable CN to go from 1 to 10, and then 10 turns into 1 (1,2,3,4,5,6,7,8,9,10,1....). This is what I have so far...
var CN:int = 1;
for(int CN = 1; CN<100; CN++);
NumberCounter.text = String(CN);
Please help. I don't get this at all :( I'm a novice programmer so a lot of things I do won't make much sense.
Your question is a bit unclear. Are you trying to go from 1 to 10 then from 10 to 1 once (20 steps) or to go back and forth between 1 and 10 in 100 steps ?
If it's the first, you can try something like this:
for(var i:int = 0, j:int = 0; i < 20; i++){
if(i < 10) j++;
else j--;
trace(j);//put this in your text field
}
if it's the second:
for(var i:int = 0; j:int = 1, k:int = 0; i < 100; i++){
if(i % 10 == 0) j *= -1; //every 10 steps flip (multiply by -1) the increment direction(increase/decrease)
k += j;//increment k based on j which will either increase or decrease
trace(k);//use this value
}
However the textfield will update right away. If you want to display this change in time you can use the ENTER_FRAME event to increment (rather than the for loop) or a tween engine to animate the value
For an infinite loop:
while(true){
for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}
To do it 10 times, which looks like your code is hinting at:
for (int x= 0; x< 10; x++){
for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}
Try this
if(CN>1)
{
var CN:int = 1;
for(int CN = 1; CN<10; CN++);
NumberCounter.text = String(CN);
}
else
{
}
var Numberofwins = 0;
CN.addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(event:Event):void{
if(CN.currentFrame == 11){
CN.gotoAndPlay(1);
}
}
import flash.events.MouseEvent;
Submit.addEventListener(MouseEvent.CLICK, CheckIf8);
function CheckIf8(event:MouseEvent):void
{
if(CN.currentFrame == 8)
{
Numberofwins++;
trace (Numberofwins);
Scorebox.text = String(Numberofwins);
}
else
{
gotoAndStop("Loose1");
}
}
This was my solution

how to make this road not scale itself.And make equally big portion(like size big) sections of the road

Im currently trying to make a road movement animation but cant figure how the road must not scale itself.
The whole code isn`t mine it is from a tutorial ,I just tried to modify it to my needs ,but I cant do the part that the road should move like if Im seeing it from above(bird view) ,at always same speed.
currently i have this swf file
http://www.stouchgames.com/APKs/Road.swf
I dont want it to be like little lines in the top and then in the bottom a big line that moves.I just want to be equally Big lines that move from top to bottom.
So far i have a simple movieClip with 2 frames and the frames have the graphics in this picture:
http://stouchgames.com/APKs/road.jpg
and this code :
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
//Depth of the visible road
private const roadLines:int = 320;
//Dimensions of the play area.
private const resX:int = 480;
private const resY:int = 320;
//Line of the player's car.
private const noScaleLine:int = 8;
//All the road lines will be accessed from an Array.
private var yMap:Array = [];
private var lines:Array = [];
private var halfWidth:Number;
private var lineDepth:int;
private const widthStep:Number = 0;
private var playerY:Number;
private var speed:int = 2;
private var texOffset:int = 100;
public function Main() {
//Populate the zMap with the depth of the road lines
for (var i:int = 0; i < roadLines; i++) {
yMap.push(1 / (i - resY));
}
playerY = 100 / yMap[noScaleLine];
for (i = 0; i < roadLines; i++) {
yMap[i] *= playerY;
}
//Make the line at the bottom to be in front of the rest,
//and add every line at the same position, bottom first.
lineDepth = numChildren;
for (i = 0; i < roadLines; i++) {
var line = new Road();
lines.push(line);
addChildAt(line, lineDepth);
line.x = resX / 2;
line.y = resY - i;
}
//Scaling the road lines according to their position
addEventListener(Event.ENTER_FRAME, race);
}
private function race(event:Event):void {
for (var i:int = 0; i < roadLines; i++) {
if ((yMap[i] + texOffset) % 100 > 50) {
lines[i].gotoAndStop(1);
} else {
lines[i].gotoAndStop(2);
}
}
texOffset = texOffset + speed;
while (texOffset >= 100) {
texOffset -= 100;
}
}
}
}
i did try to just make just 2 road movieClips with the different graphics ,then use a
for (var i:int = 0; i < 8; i++) {
if (((i % 2) == 0)) {
var line = new Road ;
lines.push(line);
addChildAt(line,lineDepth);
line.x = resX / 2;
line.y = resY - i * 50;
line.alpha = .2;
}
if (((i % 2) == 1)) {
var line = new Road2 ;
lines.push(line);
addChildAt(line,lineDepth);
line.x = resX / 2;
line.y = resY - i * 50;
line.alpha = .5;
}
but then when i get in the Event.EVERY frame function i cant make them move smoothly ... and decided it must be done by the 1st way cause of the much more detailed ending and cause in the end if i want to change the resolutions of the road i can do it much easier with it . Ofcourse i could be wrong cause i haven`t done such a thing like this before.
So can someone help ?
replace yMap[i] with i in race function:
for (var i:int = 0; i < roadLines; i++) {
if ((i + texOffset) % 100 > 50) {
lines[i].gotoAndStop(1);
} else {
lines[i].gotoAndStop(2);
}
}

How to remove/reload movieclip/graphic in Flash CS5 with AS3

I have a setupBoard(); and a setupBlocks(); in my function:
function init(e)
{
setupBoard();
removeEventListener(Event.ENTER_FRAME , init);
setupCat();
setupBlocks();
}
function setupBoard()
{
var columns:Array = new Array();
var i,j:int;
var _place:place;
for (i = 0; i < 11; i++)
{
columns = [];
for (j = 0; j < 11; j++)
{
_place = new place();
_place.thisX=i;
_place.thisY=j;
_place.thisDistance=Math.min(i+1,j+1,11-i,11-j)*11;
_place.y = 56 * i + 3;
_place.x = 5 + 71 * j + 35*(i%2);
_place.buttonMode=true;
_place.addEventListener(MouseEvent.CLICK, setBlock);
columns[j] = _place;
// SÆTTER TAL PÅ BRIKKERNE
_place.thisText.text = _place.thisDistance + " - " + _place.thisX + " : " + _place.thisY;
addChild(_place);
}
rows[i] = columns;
}
}
The "place" is the MovieClip
this function loads when the game launches and when the game is finish/completed..
the setupBoard, setup the board ofc, and the setupBlocks setup some movieclips, which contain some graphic.
Here's my question, how do I remove/reload all the blocks when the game enters that function again?
At the moment they are just placed upon each other, which I don't like at all.
If I understood correctly, what you want to do is remove all the previous blocks (from the last time you ran the setup function) when you run setup a second time.
To do that, you should create a function which loops your rows and columns Arrays, and for each Place object it find, it does the following: removes it from the stage, removes all Event Listeners, and finally sets it to null. Your function could look something like this (and you would call it just before calling setup again):
for (i = 0; i < rows.length; i++)
{
var column:Array = rows[i];
for (j = 0; j < column.length; j++)
{
var place:Place = column[j];
if (contains(place))
{
removeChild(place);
}
place.removeEventListener(MouseEvent.CLICK, setBlock);
place = null;
}
column = [];
}
row = [];
I just wrote that straight into the box, so it's not tested. But basically it does the three things necessary to get those objects removed from the view, and clears up anything that would stop them from being freed from memory by the garbage collector.
Hope that helps.
Debu