• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

fill array with for loop

Hey guys,

I want to fill an array with objects.
The objects are on my stage in Flash, they all have a name. (wall1, wall2, wall3, etc.)
Now I want those in an array, without having a list, since I have 15 walls and there might come even more.

So I thought the following.
var hwalls:Array = new Array();

for(var a = 0; a < 15; a++) {
	hwalls[a] = "walls" + a;
}

Open in new window


Then later I call it with this
for(var i = 0; i < hwalls.length; i++) {
		if(player.hitTestObject(hwalls[i])) {
			//do something
		}
	}

Open in new window


The problem is that it gives an error, and I think it has to do with the fact that I actually store the array with strings. How can I resolve this?
0
ErnstJacobs
Asked:
ErnstJacobs
  • 7
  • 6
1 Solution
 
dgofmanCommented:
You must pass a DisplayObject not String

for(var i:uint = 0; i < hwalls.length; i++) {
	var o:DisplayObject = player.getChildByName(hwalls[i]);
	if(o != null && player.hitTestObject(o)) {
		trace("You hit wall: " + hwalls[i]);
	}
}

Open in new window

0
 
ErnstJacobsAuthor Commented:
It doesn't seem to do anything, it doesn't trigger the trace.
0
 
dgofmanCommented:
It must work be sure you have children with name starting 'wall's in your player container.
add one more trace under getChildByName

trace(o)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ErnstJacobsAuthor Commented:
I think there's been a misunderstanding.

The objects I've put in are not under player, they are objects on my stage. (drag and drop from movieclip).
So I think they're not children from player.
0
 
blue-genieCommented:
a couple of things - just checking.

you mention that your objects are called "wall1" "wall2" etc
your for loop starts at 0, so would mean you need a wall0 as well (if there isn't one)

then you're adding "walls"+a  - NOT "wall" + a ---- typo here or code syntax error?
then I don't imagine that the walls are children of player -




var o:DisplayObject = player.getChildByName(hwalls[i]);

try change to

var o:DisplayObject = getChildByName(hwalls[i]);

Open in new window

0
 
ErnstJacobsAuthor Commented:
Thank you Blue-Genie, it indeed were typo's.
There is a wall0 and it goes to wall13.

I've got the following code right now
var hwalls:Array = new Array();

for(var a = 0; a < 15; a++) {
	hwalls[a] = "h_wall" + a;
}

for(var i:uint = 0; i < hwalls.length; i++) {
			var o:DisplayObject = getChildByName(hwalls[i]);
			if(o != null && player.hitTestObject(o)) {
				trace("You hit wall: " + hwalls[i]);
			}
		}

Open in new window



Right now I get this error
TypeError: Error #1034: Type Coercion failed: cannot convert "h_wall0" to flash.display.DisplayObject.
      at dimensionswitch_fla
0
 
dgofmanCommented:
Please try to add these lines on top your ActionScript file (first line)

run and check if you will get any problems
var hwalls:Array = new Array();

for(var a = 0; a < 15; a++) {
	hwalls[a] = "h_wall" + a;
}

for(var i:uint = 0; i < hwalls.length; i++) {
	var o:DisplayObject = getChildByName(hwalls[i]);
	if(o != null && o.hitTestObject(player)) {
		trace("You hit wall: " + hwalls[i]);
	}
}
		
return

Open in new window

0
 
ErnstJacobsAuthor Commented:
For some reason nothing happens.
0
 
dgofmanCommented:
var i:uint;
var hwalls:Array = new Array();
for(i = 0; i <= 13; i++){
      var hwall:DisplayObject = getChildByName("h_wall" + i);
      hwalls.push(hwall);
}
var vwalls:Array = new Array();
for(i = 1; i <= 3; i++){
      var vwall:DisplayObject = getChildByName("v_wall" + i);
      vwalls.push(vwall);
}
var enemy:Array = new Array();
for(i = 1; i <= 3; i++){
      var e:DisplayObject = getChildByName("enemy" + i);
      enemy.push(e);
}
var bullets:Array = new Array();
for(i = 1; i <= 4; i++){
      var bullet:DisplayObject = getChildByName("bullet" + i);
      bullets.push(bullet);
}
0
 
dgofmanCommented:
If you don't want to create an Array object you can validate by name

for(i = 0; i <= 13; i++){
      var hwall:DisplayObject = getChildByName("h_wall" + i);
      if(player.hitTestObject(hwall)) {
            plr_inAir = false;
            //velocityY = 0;
      }
}
0
 
ErnstJacobsAuthor Commented:
Hey dgofman,

the game is mainly finished for alpha release.
All basic functions are working, except for the last gravity problem.

The user hits a platform, can walk on it, but should fall when he leaves the platform, fall onto the next platform or out of the level ofcourse.

I find it hard to explain, I hope you understand me better when you play the game.

p.s. speed is a bit too much atm, will adjust that tomorrow, and there's a bullet bug but will fix that too tomorrow. I'm going to sleep now.

Goodnight! And thank you very much for your help so far, much appreciated.
Code-Changed-CS4.fla
0
 
dgofmanCommented:
I hope this what you want.

For future project you need to split images of the level and keep in the different frames and use gotoAndPlay(N) where N is level 1 , 2 ,3 etc
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.text.TextField;

var GRAVITY:uint = Keyboard.F1;

var mainText:TextField = menu.main_text as TextField;
var helpText:TextField = menu.help_text as TextField;
var optionsText:TextField = menu.options_text as TextField;
var continueText:TextField = menu.continue_text as TextField;
var scoreText:TextField = time as TextField;
var scoreTimer:Timer = new Timer(50, 0);
var backGround:MovieClip = backGround;

//variables player
var player:MovieClip = player;
var lastDownKeyChar:String;
var pressedKeys:Object = {};
var platform:MovieClip = null;
var IMAGE_SIZE:uint = 1250;
var VERTICAL_STEP:int;
var VERTICAL_UP:int = 10;
var VERTICAL_DOWN:int = -10;

//variables movement
var velocityY:Number = 0;
var velocityYMax:Number = 5;
var velocityYMaxReverse:Number = -5;
var acceleration:Number = 0.5;
var friction:Number = 0.89;

//variables level
var lvl_bottom:uint = 350;
var lvl_top:uint = 0;
var lvl_left:uint = 10;

//Bullets
var bullet1:MovieClip = bullet1;
var bullet2:MovieClip = bullet2;
var bulletTimer:Timer = new Timer(1000, 100);
var lastBulletCount:uint;

//arrays
var hwalls:Array = new Array();
for(var hw = 0; hw <= 21; hw++){
      var hwall:DisplayObject = getChildByName("hwall" + hw);
      hwalls.push(hwall);
}
var vwalls:Array = new Array();
for(var vw = 1; vw <= 11; vw++){
      var vwall:DisplayObject = getChildByName("vwall" + vw);
      vwalls.push(vwall);
}

var enemy:Array = new Array();
for(var en = 1; en <= 25; en++){
      var e:DisplayObject = getChildByName("enemy" + en);
      enemy.push(e);
}


//initiate
showMenu(true);
mainText.text = "Dimension Switch";
stage.frameRate = 100;
for(var i = 0; i < enemy.length; i++) {
	enemy[i].visible = false;
}

//Events
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
scoreTimer.addEventListener(TimerEvent.TIMER, getScore);
bulletTimer.addEventListener(TimerEvent.TIMER, shootBullet);

//functions
function showMenu(b:Boolean) {
	menu.visible = b;
	mainText.visible = true;
	optionsText.visible = true;
	helpText.visible = false;
	continueText.visible = false;
	stage.removeEventListener(Event.ENTER_FRAME, onTick);
	scoreTimer.stop();			
}

function onTick(e:Event) {
	playerMove();
	getGravity();
	moveBullet();
	playerCollide();
}

function getScore(event:TimerEvent) {
	scoreText.text = "Time: " + scoreTimer.currentCount.toString();
}

function reset() {
	//reset variables
	mainText.text = "Dimension Switch";
	scoreTimer.reset();
	player.x = 15;
	player.y = 150;
	
	platform = null;
	VERTICAL_STEP = VERTICAL_UP;
	scoreTimer.start();
	
	if(backGround.x != IMAGE_SIZE){
		backGround.x = IMAGE_SIZE;
		for(var i = 0; i < hwalls.length; i++) {
			hwalls[i].x += IMAGE_SIZE;
		}
		for(var c = 0; c < vwalls.length; c++) {
			vwalls[c].x += IMAGE_SIZE;
		}
		for(var e = 0; e < enemy.length; e++) {
			enemy[e].x += IMAGE_SIZE;
		}
		finish.x += IMAGE_SIZE;
	}
	bullet1.visible = false;
	bullet2.visible = false;
}

function getGravity() {
	if(!platform) {
		if(VERTICAL_STEP == VERTICAL_UP) {
			velocityY -= 0.5;
		}else{
			velocityY += 0.5;
		}
		velocityY *= friction;
		player.y -= velocityY;
	}
} //getGravity

function playerCollide() {
	for(var i = 0; i < hwalls.length; i++) {
		if(platform != hwalls[i] && player.hitTestObject(hwalls[i])) {
			platform = hwalls[i];
		}
	}
	for(var c = 0; c < vwalls.length; c++) {
		if(player.hitTestObject(vwalls[c])) {
			player.x -= 2;
		}
	}
	for(var e = 0; e < enemy.length; e++) {
		if(player.hitTestObject(enemy[e])) {
			showMenu(true);
			mainText.text = "Game Over";
		}
	}
	if(player.hitTestObject(bullet1) && player.hitTestObject(bullet2)) {
		mainText.text = "Game Over";
		menu.visible = true;
	}
	if(player.hitTestObject(finish)) {
		showMenu(true);
		mainText.text = "You Win time " + scoreTimer.currentCount; 
	}
	if(player.y <= lvl_top || player.y >= lvl_bottom) {
		showMenu(true);
		mainText.text = "Game Over";
	}
} //playerCollide

function playerMove() {
	
	//Move Left or Right
	if(pressedKeys[Keyboard.RIGHT] || pressedKeys[Keyboard.LEFT]){
		if(pressedKeys[Keyboard.LEFT] == true) {
			player.x -= 2;
		}else if(pressedKeys[Keyboard.RIGHT] == true) {
			player.x += 2;
		}
		if(platform != null && !player.hitTestObject(platform)){
			platform = null;
		}
	}

	//And can be GRAVITY
	if(pressedKeys[GRAVITY] == true){
		if(platform != null){
			if(VERTICAL_STEP == VERTICAL_UP) {
				VERTICAL_STEP = VERTICAL_DOWN;
			}else {
				VERTICAL_STEP = VERTICAL_UP;
			}
			player.y += VERTICAL_STEP;
			platform = null;
		}
	}
	velocityY *= friction;
	moveLevel();

	if(player.x <= lvl_left) {
		player.x += 2;
	}
} //movePlayer

function moveLevel() {	
	if(player.x > IMAGE_SIZE) {
		backGround.x -= IMAGE_SIZE;
		
		for(var i = 0; i < hwalls.length; i++) {
			hwalls[i].x -= IMAGE_SIZE;
		}
		for(var c = 0; c < vwalls.length; c++) {
			vwalls[c].x -= IMAGE_SIZE;
		}
		for(var e = 0; e < enemy.length; e++) {
			enemy[e].x -= IMAGE_SIZE;
		}
		
		finish.x -= IMAGE_SIZE;
		player.x = 0;
	}
}

function keyPressedDown(e:KeyboardEvent) {
	var char:String = String.fromCharCode(e.charCode).toUpperCase();
	
	if(char != "C" && (lastDownKeyChar == "H")){ //wait till user will not press continue key
		return;
	}
	
	switch(char){
		case "H":
			showMenu(true);
			helpText.visible = true;
			optionsText.visible = false;
			mainText.visible = false;
			continueText.visible = true;
			break;
		case "Q":
			showMenu(true);
			fscommand("quit", "");
			break;
		case "C":
			if(lastDownKeyChar == "H" || lastDownKeyChar == "Q"){
				showMenu(false);
				stage.addEventListener(Event.ENTER_FRAME, onTick);
				scoreTimer.start();
				break;
			}
			else{}
		case "N":
			showMenu(false);
			reset();
			stage.addEventListener(Event.ENTER_FRAME, onTick);
			bulletTimer.start();
			break;
	}
	
	if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) {
		pressedKeys[e.keyCode] = true;
	}else if(char == "G"){
		pressedKeys[GRAVITY] = true;
	}
	lastDownKeyChar = char;
} //keyPressedDown

function keyPressedUp(e:KeyboardEvent):void {
	var char:String = String.fromCharCode(e.charCode).toUpperCase();
	if(char == "G"){
		delete pressedKeys[GRAVITY];
	}else{
		delete pressedKeys[e.keyCode];
	}
} //keyPressedUp

function setBullets() {
	if(player.x < IMAGE_SIZE) {
		bullet1.x = 673;
		bullet1.y = 215;
		bullet2.x = 689;
		bullet2.y = 216;
	}
	if(player.x > IMAGE_SIZE) {
		bullet1.x = 407;
		bullet1.y = 169;
		bullet2.x = 407;
		bullet2.y = 226;
	}
} //setBullets

function shootBullet(event:TimerEvent) {
	var tempCount:uint = bulletTimer.currentCount;
	
	if(tempCount > (lastBulletCount + 2)) {
		setBullets();
		bullet1.visible = true;
		bullet2.visible = true;
		lastBulletCount += 2;
	}
} //shootBullet

function moveBullet() {
		if(player.x < IMAGE_SIZE) {
			bullet1.x -= 2;
			bullet2.x -= 2;
		}
		if(player.x > IMAGE_SIZE) {
			bullet1.x += 5;
			bullet2.x += 5;
		}	
} //moveBullet

Open in new window



0
 
dgofmanCommented:
Change your timer to shoot all the time

var bulletTimer:Timer = new Timer(1000, 0);

My best time is 596
0
 
ErnstJacobsAuthor Commented:
You're a brilliant man! Thank you so much.

My time: 639... Someday I will beat you at my own game! hehe
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now