Creating a maze in actionscript3 - actionscript-3

I4d like to create a maze in actionscript 3 but without drawing it because a lot of people are saying that timeline code and drawing things is bad so i'd like to create it with an array. I've searched on google and looked over a lot of tutorials, all doing it differently but not one of them uses classes and all that stuff and I'd really like to do it. I have the idea of how to do it, using an array filled with different numbers of characters if there's a wall, nothing etc... And i know how to draw the block with the graphics things then put a if loop and if the number is 0 put nothing if the number is 1 create the block and place it, but then i'm a bit lost on HOW to make the block appear at the same spot where there is a 1 in the array, I looked at tuts where they did something with rows but I couldn't really understand it clearly.
And also I'm not sure if i have to create a new class for the block, and what do I have to put in this class if i do create it? Do i need to create the block in the class, or outside of it? =/
If someone knows what I mean then all help is welcome.
If you need more details please tell me, sorry if it's confused. =3

It looks like your best bet, your path of least resistance in the long run, may lie in doing a little bit more study and some smaller programs before embarking on this. However if you want a 2D maze made with an Array, you could just do this:
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = 1;
m_arrMaze[0][1] = 1;
.
.
.
m_arrMaze[24][24] = 3;
.
.
.
m_arrMaze[49][49] = 0;
}
This is because you seemed to mention using an array and setting its elements to certain int values to denote what each little spot or room or whatever in the maze is or has. The reason a lot of tutorials may not use a whole lot of classes is because, if this is all you're doing with it, you really don't need too many different classes to denote the stuff in the maze. Just instead of using hard-coded int values, go ahead and put them in constants at the top of your maze class:
private static const EMPTY_SPACE:int = 0;
private static const WALL:int = 1;
.
.
.
private static const PLAYER:int = 3;
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = WALL;
m_arrMaze[0][1] = WALL;
.
.
.
m_arrMaze[24][24] = PLAYER;
.
.
.
m_arrMaze[49][49] = EMPTY_SPACE;
}
If each type of contents within the maze is liable to have a whole different set of nouns, verbs, and adjectives associated with it, instead of just being a different type of marker of where something's at like in the examples above, and if the program is going to do a lot of different things with those contents, that's when you want to use a whole bunch of different classes. Hopefully this will get you started.

Related

how to connect a number variable to dynamic text in actionscript 3.0?

i know this might be simple but i have been searching everywhere for a fix but i just cannot find it!
i want to make something like a health #, so when you press whatever button the dynamic text # will go up or down. on my test project i have two layers, the first with the following code
var hp:Number = 100;
health.text = String hp;
hp being the variable, and health being the dynamic text. then i have the next layer with the button with:
function button(e:MouseEvent):void
{
hp -= 10;
}
without that second chunk of code, the dynamic text will appear, but once that is added it will disappear and the button is function-less.
how do i make this work??? once again sorry if this is a dumb question, i'm just very stumped.
The accepted answer is good, but I wanted to point out that your original code was actually very close to being correct, you just needed parenthesis:
health.text = String(hp);
For most objects String(object) and object.toString() has the same effect, except that object.toString() throws an error if object is null (which could be desirable or undesirable, depending on what you expect it to do).
This is not correct:
health.text = String hp;
use:
health.text = hp.toString();
and:
function button(e:MouseEvent):void
{
hp -= 10;
health.text = hp.toString();
}

How to Use .as Exported File from PhysicsEditor

The question was here for a long time with bounty and no satisfying solution for me. I erased the first post and am posting instead a question that can be answered quickly with a yes or no so I can proceed with my doings.
If you could answer it really fast before it's deleted by "not a good question". Is using a custom shape from PhysicsEditor to Nape the same as doing it with Box2D? (ofc changing syntax)
If you could then give a look in that link then say it's the same process in Nape that'll be enought thanks.
I ask this because I found the Box2D tutorial easier to follow so far.
public var floor:Body;
floor = new Body(BodyType.STATIC);
var floorShape:PhysicsData = new PhysicsData();
floor.shapes.add(floorShape); // Error: Implicit coercion of a value of type PhysicsData to an unrelated type nape.shape:Shape.
floor.space = space;
Update:
According to a comment on this blog post, it sounds like recent versions of Nape have broken compatibility with the physics editor. Specifically, the graphic and graphicUpdate properties no longer exist on body objects. The solution suggested is to remove references to those properties.
I'm not in a position to be able to test this, but you could try updating the createBody method of your floor class as follows:
public static function createBody(name:String /*,graphic:DisplayObject=null*/):Body {
var xret:BodyPair = lookup(name);
//if(graphic==null) return xret.body.copy();
var ret:Body = xret.body.copy();
//graphic.x = graphic.y = 0;
//graphic.rotation = 0;
//var bounds:Rectangle = graphic.getBounds(graphic);
//var offset:Vec2 = Vec2.get(bounds.x-xret.anchor.x, bounds.y-xret.anchor.y);
//ret.graphic = graphic;
/*
ret.graphicUpdate = function(b:Body):void {
var gp:Vec2 = b.localToWorld(offset);
graphic.x = gp.x;
graphic.y = gp.y;
graphic.rotation = (b.rotation*180/Math.PI)%360;
}
*/
return ret;
}

Can't reference a child with a variable instance name [syntax]

I want to generate villagers for my game and then organize them into a list but I'm having a bit of difficulty, here is what it's supposed to look like and here is what it actually looks like. The code for generating kinsmen is:
var k = new kinsmen ;
menuArea.kinsmenDivider.addChild(k);
totalKinsmen++;
totalKinsmenAlive++;
k.name = "kinsmen" + totalKinsmen;
The code used for sorting them is:
for (var i:int = 1; i < (totalKinsmen+1); i++) {
if (menuArea.kinsmenDivider.getChildByName("kinsmen"+i) !=null) {
menuArea.kinsmenDivider["kinsmen" + i].y = menuItemCount * 107.5;
menuItemCount++;}}
That should look through the ID's of every kinsmen that's ever existed and when it finds the ID of one that's alive it should give it a place in the menu and continue going through the other IDs.
trace ("kinsmen" + totalKinsmen);
trace (menuArea.kinsmenDivider.getChildByName("kinsmen"+i));
Both always have the same values in my tests. The debugger posts the following error during runtime:
TypeError: Error #1010: A term is undefined and has no properties.
When I remove ["kinsmen" + i] it stops giving errors and the entire menu moves down each time a new kinsmen is generated so I know that's the part causing the issue but I don't know why or how to fix it and would appreciate help, all the above code is in my document class. I only started coding AS3 a few days ago so I'm still a bit shaky. Cheers for reading.
You access the clip correctly everywhere except
menuArea.kinsmenDivider["kinsmen" + i].y
shouldn't it be :
menuArea.kinsmenDivider.getChildByName("kinsmen" + i).y

Can you help me make more OOP abstraction with this code?

This code is not java code, and I'm not getting any answer from ActionScript developers. So I tagged it with java, but Action Script is similar to java and this an OOP question.
I'm using Grid Data and I want to accomplish this following task:
Method 1: I want to multiply each row Row1num1 * Row1num2 and so on,
var Row1num1:String;
var Row2num2:String;
var Row2num1:String;
var Row2num2:String;
var Row3num1:String;
var Row3num2:String;
var event1:Object={num1:Row1num1,num2:Row1num2};
var event2:Object={num1:Row2num1,num2:Row2num2};
var event3:Object={num1:Row3num1,num2:Row3num2};
then add them to a dataGrid
dataGrid.columns =["num1","num2"];
dataGrid.addItem(event1);
dataGrid.addItem(event2);
dataGrid.addItem(event3);
but by using this method, if I have 20 rows, I will have a lot of variables, obviously it's bad.
method 2: In this method creating Grid Data rows at runtime and multiply them.
//button to add rowGrid
dd.addEventListener(MouseEvent.CLICK,ddd);
var numm:String="34";
function ddd(evt:MouseEvent):void
{
var event4:Object={num1:Rownum1,num2:Rownum2};
dataGrid.addItem(event4);
}
but when I use this method, I have a hard time accessing each row data and multiply them.
This example because I'm creating GPA calculator and I want to take each row credit Hours and multiply them with the scale value at the same row, first method is bad because there's not abstraction .
The second method what I'm hoping to work ,because I want user to add row depend on their number of courses.
I hope my English is not bad.
I hope my question don't get vote down, and by reading this question can you determine what I'm missing so I can learn it .
And is there any tutorial I can use to solve my problem?
I'm just addressing your first method for now, but it almost seems at though you want an array of some sort.
Here's a link on how to use Actionscript arrays.
If you need more dimensions, you can make an array of arrays. This will help you cut down on the number of variables.
I hope I correctly understood your question. I'll give it a go either way...
So, one of the best things about actionscript in comparison to most other strongly-typed Object-Oriented languages is how easy reflection is (probably thanks to its javascript origins).
That being said, what you can do is simply create an array using a "for" loop. What I am assuming is that the variables row1Num1 row2Num2 and so on already exist in your class. Otherwise, obviously it would be much more efficient to store them in an array and simply read from it into a new array. Anyhow, the code should look something like this:
method 1:
var eventsArr:Array = [];
for(var i:int = 1; this["row" + i + "Num1"] != undefined /*or i<=length*/; i++){
eventsArr.push({num1:this["row" + i + "Num1"], num2:this["row" + i + "Num2"]});
}
for(var j:int = 0; j < eventsArr.length; j++){
dataGrid.addItem(eventsArr[j]);
}
method 2:
dd.addEventListener(MouseEvent.CLICK,ddd);
var numm:String="34"; //I am assuming this refers to the row number you wanted to add.
function ddd(evt:MouseEvent):void
{
var event4:Object={num1:this["row" + numm + "Num1"],num2:this["row" + numm + "Num2"]};
eventsArr.push(event4);
dataGrid.addItem(event4);
}
Hope that helps.

Re-stacking MovieClips in an Array

I was trying to make a similar thing with the game SameGame (ie. the block above the removed blocks fall downward). Before trying this with an Array that contains MovieClips, this code worked (tried it with int values). With MovieClips on the array, it seems not working the same way.
With int values, example:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After: 1,2,3,4,6,7,8,9,10
But with MovieClips:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After; 1,2,3,4
// Assume the numbers are movieclips XD
Basically, it strips everything else, rather than just the said block >_<
Here's the whole method. Basically, two extra arrays juggle the values above the soon-to-be removed value, remove the value, then re-stack it to the original array.
What could be wrong with this? And am I doing the right thing for what I really wanted to emulate?
function popUp(col:uint, row:uint)
{
var tempStack:Array = new Array();
var extraStack:Array = new Array();
tempStack = IndexArray[col];
removeChild(tempStack[0]);
for(var ctr:uint = tempStack.length-(row+1); ctr > 0; ctr--)
{
removeChild(tempStack[ctr]);
extraStack.push(tempStack.pop());
trace(extraStack);
}
tempStack.pop();
for(ctr = extraStack.length; ctr > 0; ctr--)
{
tempStack.push(extraStack.pop());
//addChild(tempStack[ctr]);
}
IndexArray[col] = tempStack;
}
PS: If it's not too much to ask, are there free step-by-step guides on making a SameGame in AS3 (I fear I might not be doing things right)? Thanks in advance =)
I think you just want to remove an element and have everything after that index shift down a place to fill what you removed. There's an inbuilt function for this called splice(start:uint, length:uint);
Parameters:
start - the index to start removing elements from
length - the amount of elements to remove
var ar:Array = ["hello","there","sir"];
ar.splice(1, 1);
ar is now -> ["hello", "sir"];
As per question:
Here's an example with different types of elements:
var ar:Array = [new MovieClip(), "some string", new Sprite(), 8];
ar.splice(2, 1);
trace(ar); // [object MovieClip], some string, 8
And further example to display the indexes being changed:
trace(ar[2]); // was [object Sprite], is now 8