11-08-2008, 12:57 AM
|
#1
|
|
Registered User
Join Date: Nov 2008
Posts: 5
|
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 doesnt 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("");
|
|
|
11-08-2008, 10:37 AM
|
#2
|
|
Senior Member
Join Date: Sep 2006
Location: Poland, EU
Posts: 268
|
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);
|
|
|
11-08-2008, 03:12 PM
|
#3
|
|
Registered User
Join Date: Nov 2008
Posts: 5
|
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!
|
|
|
11-08-2008, 08:19 PM
|
#4
|
|
Senior Member
Join Date: Sep 2006
Location: Poland, EU
Posts: 268
|
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.
|
|
|
11-09-2008, 02:56 PM
|
#5
|
|
Registered User
Join Date: Nov 2008
Posts: 5
|
brilliant.
thanks for all your help!
|
|
|
11-09-2008, 03:26 PM
|
#6
|
|
Senior Member
Join Date: Sep 2006
Location: Poland, EU
Posts: 268
|
Quote:
Originally Posted by AlanH
brilliant.
thanks for all your help! 
|
Cool. Which university are you at?
Last edited by jasonJ; 11-09-2008 at 05:17 PM.
|
|
|
11-10-2008, 11:08 AM
|
#7
|
|
Registered User
Join Date: Nov 2008
Posts: 5
|
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.
|
|
|
11-22-2008, 02:32 AM
|
#8
|
|
Registered User
Join Date: Nov 2008
Posts: 5
|
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!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 06:42 PM.
///
|
|