Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-08-2008, 12:57 AM   #1
AlanH
Registered User
 
Join Date: Nov 2008
Posts: 5
Default Trying to trace a stage in output for a simulation..

Hi,

I am having real trouble doing a part of a simulation for some uni coursework, and I need some help.
This is the section I am stuck on. I have been looking at it for 48 hours now, and have been close to smashing my mac because I am so frustrated.


Quote:
The first line is “*” followed by stageWidth “*”s, that is for example one followed by 55 *s There then follows one line for each line of the stage up to stageHeight (= 40 to start with). Each line consists of * followed by stageWidth (55) underscores, or “_”. When you get to the (x, y) coordinates of the alien, replace the _ by “A”. Similarly print G for gun and B for bullet. Finish off with a final line of * followed by stageWidth (55) *s. Note: if the gun doesn’t fire, the bullet will still be at (0, 0) and wont print out. gun end position = (30, 40) alien end position = (35, 31) bullet end position = (30, 17)

************************************************** ****** *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_____________________________B___________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *__________________________________A______________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_________________________________________________ ______ *_____________________________G___________________ ______
************************************************** ******


I have x & y positions for a bullet (if one fires in the simulation) stored, and also the alien x & y co-ords.
The gun is fixed in position of x = 30 y = 40.

I need to know, how & if it is possible to trace a line of characters to the output window, then how to change underscores for letters to indicate the gun, alien & bullet positions.


the rest of my code is -


Attach Code

Code:
var alienXpos: Number = 5 ;																	//alien x postion on stage
var alienYpos: Number = 1;																	//alien y postion on stage
var nCycles: Number = 30 ;																	//cycles to represent frame
var gunXpos: Number = 30 ;																	//gun x & y stage variable
var gunYpos: Number = 40 ;
var bulletXpos: Number = 0 ;																//Bulelt x & y position
var bulletYpos: Number = 0 ;
var fireGun: Number = 0 ;																	//Gun fire random number variable
var fireGunStore: Number = 0 ;																//Store for a fire number
var Counter: Number = 1; 																	
var fireFlag: Boolean = false ;																//Bullet boolean - true if above 0.95
var stageWidth: Number = 55 ;
var stageHeight: Number = 40 ;

	
trace("Alien X start Position = "+alienXpos);												//trace starting x & y value of alien
trace("Alien Y start Position = "+alienYpos);
trace("Gun X start Position = "+gunXpos);													//trace starting c & y value of gun
trace("Gun Y start Position = "+gunYpos);
trace("   ");

while (Counter<=nCycles){																	//Counter less than or equal to 30
	if (fireFlag == false){																	//If fire flag is wquivalnt to false, then
		fireGun = Math.random(0, 1) ;														//Generate a random number between 0 - 1
		if (fireGun>0.95) {																	//If the fireGun vlaue is greater than 0.95  
			fireGunStore = fireGun;															
			//store it
			fireFlag = true;																//set the fireflag boolean to true
			bulletXpos = gunXpos;															//Bullet position is the same as gun position
			bulletYpos = gunYpos;															
		}
		else if (fireFlag == false) {														//else/also if the boolean is false
			alienXpos++;																	//increase alie x & y position
			alienYpos++;		
			Counter++;																		//Counter increase bt 1 
			trace("alien position, x/y = "+alienXpos+"/"+alienYpos);						//trace alien position
		}
	} else if (fireFlag == true) {															//if the boolean is true,
		alienYpos++;																		// update the x & y alien position
		alienXpos++;
		bulletYpos--;																		// take one from the y position (move in the opposite direction)
		Counter++;																			//increase counter by 1
		trace("alien position, x/y = "+alienXpos+"/"+alienYpos+
			  " bullet x/y = "+gunXpos+"/"+bulletYpos);
	}
}

trace("Alien X End Position = "+alienXpos);													//trace ending x and y of gun & alien
trace("Alien Y End Position = "+alienYpos);
trace("Gun X End Position = "+gunXpos);
trace("Gun Y End Position = "+gunYpos);
trace("Bullet X End Position = "+bulletXpos);
trace("Bullet Y End Position = "+bulletYpos);
trace("   ");
trace("");
AlanH is offline   Reply With Quote
Old 11-08-2008, 10:37 AM   #2
jasonJ
Senior Member
 
Join Date: Sep 2006
Location: Poland, EU
Posts: 268
Send a message via ICQ to jasonJ
Default

Quote:
I need to know, how & if it is possible to trace a line of characters to the output window
What do you mean by a line of characters? Do you need to draw (in the output panel) a text board similar to the one at the beginning of your post?

Quote:
then how to change underscores for letters to indicate the gun, alien & bullet position
Use the String class's methods. Or use this function I found in a book:
ActionScript Code:
String.prototype.replace = function (search:String, replace:String):String {   var temp:String = new String();   var searchIndex = -1;   var startIndex = 0;   while ((searchIndex = this.indexOf(search, startIndex)) != -1) {     temp += this.substring(startIndex, searchIndex);     temp += replace;     startIndex = searchIndex + search.length;   }   return temp + this.substring(startIndex); }; //usage var s:String = "My name _is_ Jason J."; trace(s); s = s.replace("Jason", "James"); trace(s); s = s.replace("_", "G"); trace(s);
jasonJ is offline   Reply With Quote
Old 11-08-2008, 03:12 PM   #3
AlanH
Registered User
 
Join Date: Nov 2008
Posts: 5
Default

sorry for the slightly confusing post. The idea is to trace the text board (like at the beginning of the post) and then change the relevant underscores (which are meant to represent x, y co-ords) with A for the alien end coordinates, G for the Gun Co-Ords and then B for the bullet ending CO-Ords.

cheers for your help, I'll give this a try!
AlanH is offline   Reply With Quote
Old 11-08-2008, 08:19 PM   #4
jasonJ
Senior Member
 
Join Date: Sep 2006
Location: Poland, EU
Posts: 268
Send a message via ICQ to jasonJ
Default

Try this (just copy and paste):
ActionScript Code:
var board:Array = new Array(); var boardRows:Number = 40; var boardCols:Number = 50; var gun:Object = new Object({x:30, y:40}); var alien:Object = new Object({x:20, y:20}); var bullet:Object = new Object({x:15, y:30}); function drawBoard():String {     var boardString:String = new String();         board[0] = new Array();     for (var col:Number = 0; col < boardCols; col++) {         board[0][col] = "*";     }     boardString += board[0].join("") + "\n";         for (var row:Number = 1; row <= boardRows; row++) {         board[row] = new Array();         board[row][0] = "*";         for (var col:Number = 1; col < boardCols; col++) {             if (col == gun.x && row == gun.y) {                 board[row][col] = "G";             }             else if (col == alien.x && row == alien.y) {                 board[row][col] = "A";             }             else if (col == bullet.x && row == bullet.y) {                 board[row][col] = "B";             }             else {                 board[row][col] = "_";             }         }         boardString += board[row].join("") + "\n";     }         board[boardRows+1] = new Array();     for (var col:Number = 0; col < boardCols; col++) {         board[boardRows+1][col] = "*";     }     boardString += board[boardRows+1].join("") + "\n";         return boardString; } trace(drawBoard());

It may not be the solution, it's just a hint that may be useful. It's pretty self-explanatory so I don't think a comment is necessary.
jasonJ is offline   Reply With Quote
Old 11-09-2008, 02:56 PM   #5
AlanH
Registered User
 
Join Date: Nov 2008
Posts: 5
Default

brilliant.

thanks for all your help!
AlanH is offline   Reply With Quote
Old 11-09-2008, 03:26 PM   #6
jasonJ
Senior Member
 
Join Date: Sep 2006
Location: Poland, EU
Posts: 268
Send a message via ICQ to jasonJ
Default

Quote:
Originally Posted by AlanH View Post
brilliant.
thanks for all your help!
Cool. Which university are you at?

Last edited by jasonJ; 11-09-2008 at 05:17 PM.
jasonJ is offline   Reply With Quote
Old 11-10-2008, 11:08 AM   #7
AlanH
Registered User
 
Join Date: Nov 2008
Posts: 5
Default

the Robert Gordon University in Aberdeen.

We have been learning AS2.0 this semester and our tutor dropped that nugget on us for coursework. I won't be using it as a direct answer, but it will certainly help me understand and compile my own answer.
AlanH is offline   Reply With Quote
Old 11-22-2008, 02:32 AM   #8
AlanH
Registered User
 
Join Date: Nov 2008
Posts: 5
Default

If any one is interested.. this is the solution me & a friend came up with.

Code:
var stageWidth: Number = 55 ;
var stageHeight: Number = 40 ;
var alienXpos: Number = 5 ;																	//alien x postion on stage
var alienYpos: Number = 1 ;																	//alien y postion on stage
var nCycles: Number = 30 ;																	//cycles to represent frame
var gunXpos: Number = Math.floor(stageWidth/2) ;																	//gun x & y stage variable
var gunYpos: Number = stageHeight ;
var bulletXpos: Number = 0 ;																//Bulelt x & y position
var bulletYpos: Number = 0 ;
var fireGun: Number = 0 ;																	//Gun fire random number variable
var Counter: Number = 1; 																	
var fireFlag: Boolean = false ;																//Bullet boolean - true if above 0.95

trace("Alien X start Position = " +alienXpos);												//trace starting x & y value of alien
trace("Alien Y start Position = " +alienYpos);
trace("Gun X start Position = " +gunXpos);													//trace starting c & y value of gun
trace("Gun Y start Position = " +gunYpos);
trace("");

while (Counter <= nCycles){																	//Counter less than or equal to 30
	if (fireFlag == false){																	//If fire flag is wquivalnt to false, then
		fireGun = Math.random(0, 1) ;														//Generate a random number between 0 - 1
		if (fireGun > 0.95) {																//If the fireGun vlaue is greater than 0.95  
			fireFlag = true;																//set the fireflag boolean to true
			bulletXpos = gunXpos;															//Bullet position is the same as gun position
			bulletYpos = gunYpos;															
		}
		else if (fireFlag == false) {														//else/also if the boolean is false
			alienXpos ++;																	//increase alien x & y position
			alienYpos ++;		
			Counter ++;																		//Counter increase by 1 
			trace("alien position, x/y = " +alienXpos +"/" +alienYpos);						//trace alien position
		}
	} else if (fireFlag == true) {															//if the boolean is true,
		alienYpos ++;																		// update the x & y alien position
		alienXpos ++;
		bulletYpos --;																		// take one from the y position (move in the opposite direction)
		Counter ++;																			//increase counter by 1
		trace("alien position, x/y = "+alienXpos +"/" +alienYpos +" bullet x/y = " +bulletXpos +"/" +bulletYpos);

		}
}

trace("Alien X End Position = " +alienXpos);													//trace ending x and y of gun & alien
trace("Alien Y End Position = " +alienYpos);
trace("Gun X End Position = " +gunXpos);														//gun
trace("Gun Y End Position = " +gunYpos);
trace("Bullet X End Position = " +bulletXpos);													//bullet finish position
trace("Bullet Y End Position = " +bulletYpos);
trace("Bullet is active = " +fireFlag +" and position is (x, y) = " +bulletXpos +" , " +bulletYpos);	
trace("");
trace("");

/* String vars part 4 - 
var starLine:String = "************************************************************";
var scores:String = "*––––––––––––––––––––––––––––––––––––––––––––––––––";
var alienString:String = "*"+"______________________________" +"a" +"________________________";
var bulletString:String = "*"+"_____________________________" +"b" +"_________________________";
var gunString:String = "*"+"_____________________________" +"g" +"_________________________";
var explosionString:String = "*"+"_____________________________" +"#" +"_________________________";
*/

function scores(n:Number):String{																			//function scores - sets underscores
	var c:String = "";																						//declare local variant c as a string
	for (i = 0; i < n; i++){																				//declare for loop for function - passes n a number to loop
		c = c+"_";																							//c is equal to c plus underscores for the value of n - i.e. stageWidth!
	}
	return c;																								//return the value of c after the loop has finished
}

function stars(n:Number):String{																			//function stars - sets stars
	var b:String = "";																						//declare local var b as a string
	for (j = 0; j < n; j++){																				//declare for loop for function - passes n, a number to loop
		b = b+"*";																							//b is equal to b plus underscores for the value of n - i.e. stageWidth
	}
	return b;																								//retuen the value of b after the loop has finished. 
}

trace (stars(stageWidth+1));
var k:Number;
for (k = 1; k <= stageHeight; k ++){
	if (k == alienYpos && k == bulletYpos){
		trace ("*" +scores(gunXpos-1) +"#" +scores(stageWidth-gunXpos));
	}
	else if (k == gunYpos && k == alienYpos){
		trace("*" +scores(alienXpos-1) + "a" +scores(gunXpos-alienXpos-1)+"g" +scores(stageWidth-gunXpos));
	}
	else if (k == bulletYpos){
		trace("*" +scores(bulletXpos-1) + "b" + scores(stageWidth-bulletXpos));
	}
	else if (k == alienYpos){
		trace("*" +scores(alienXpos-1) + "a" + scores(stageWidth-alienXpos));
	}
	else if (k == gunYpos){
		trace ("*" +scores(gunXpos-1) +"g" +scores(stageWidth-gunXpos));
	}
	else {
		trace ("*" +scores(stageWidth)); 
	};
};
trace (stars(stageWidth+1));
YAY!
AlanH is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
attachMovie kills MovieClip subclass objects Flash Gordon ActionScript 2.0 10 05-17-2007 07:03 AM
A specific map zoom out boundary Q DoubleE ActionScript 1.0 (and below) 2 01-17-2007 12:53 PM
trace() ouput to file Deemo24 ActionScript 2.0 8 08-30-2006 09:44 AM
childNodes[x] doesn't work in flash5? miiiriam ActionScript 1.0 (and below) 1 07-04-2006 06:27 PM
trace output missing battleline Flash 8 General Questions 6 06-24-2006 12:01 AM


All times are GMT. The time now is 09:09 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.