Starting out with logic ai

Ok i think i’ll start out with the logic ai because i pretty much know how to do the others (to a degree at least!) but anyway, first i need to set up the field, but to join the dots i need to enlist the aid of the drawingAPI which i’m not to familiar with, neway here’s my fla.

_root.onEnterFrame = function() { 
	//drawing line
	clear();
	//initial
	dotA._x = 50;
	dotA._y = 50;
	dotB._x = 100;
	dotB._y = 100;
	//drawing
	lineStyle(1, 0xFF0000);
	moveTo(dotA._x, dotA._y);
	lineTo(dotB._x, dotB._y);
};

but i’m not sure why it won’t draw a line. later on when i get this drawing just any line down i’ll put dotA = root[“t”+1+""+1] and dotB = root[“t”+2+""+2] or something just as initial and then you can move the lines around with arrow keys. but yeah neone can explain why? I’ve also tried just

lineStyle(1, 0xFF0000); 
	moveTo(5, 5);
	lineTo(100, 100);

which doesn’t work either! Many thanks

No offence, but we don’t need your stuff (though it is good). Marz has covered much on the subject and explained it in a much more understandable way.

Huh, i’m trying to use marz’s stuff, i have no knowledge of logic based ai, but i need to make the basics of the game before i can implement his logic, but i get stuck on the drawingAPI

Ahh my friend… Before you can create lines and the such… You need to create a median to actually create the lines on… Such as an emptyMovieClip


_root.onLoad =function()
{
   //initial
   dotA._x = 50;
   dotA._y = 50;
   dotB._x = 100;
   dotB._y = 100;
   //drawing
   _root.createEmptyMovieClip("line1", 100);
   lineStyle(1, 0xFF0000);
   moveTo(dotA._x, dotA._y);
   lineTo(dotB._x, dotB._y);
}

Give that a try…bit. tinkle around with it for a bit.

Whoa i’m totally tripping, i’d already tried making an empty movie clip like what u did, but i didn’t work. But i thought marz knows what he’s talking about, so i copied your code into my fla, didn’t work. So then i copied it into an empty fla file, and it worked! There must have been something blocking the drawing in the old fla, or else it was corrupted, fully weird!

BTW Marz you don’t need the _root.createEmtpyMovieClip, because the lines get called into the root anyway, and in your post, you didn’t put the lines in the empty clip anyway, don’t you need _root[“lines1”].lineStyle(); etc.

Thanx for your help marz, now i can try ur tute!

Ok i’ve nearly got the line drawing down, ie the line moving around for just human player, and i’ll fix the rest of it later, but i’m stuck getting information back out of an array. see my fla if you can’t follow me.
To store a line when pressing space i put it in an array as follows

dotsToBeJoined = [[dotBx, dotBy], [dotAx, dotAy]];
    	    joinTheDots(dotsToBeJoined);

and then the joinTheDots function is as follows:

function joinTheDots(someArray) {
	dotArray = dotArray.concat(someArray);
	trace(dotArray);
	// turn array into a line
	for (var i = 0; i<(dotArray.length+1); i++) {
		//divide into sets of four
		//not sure how to put back
		//into dot form?!??
		dotArray[i/4] = blah;
		pointA = _root["t_"+(dotAx)+"_"+(dotAy)];
		pointB = _root["t_"+(dotBx)+"_"+(dotBy)];
		_root.createEmptyMovieClip("grid", 1000);
		_root["grid"].lineStyle(5, 0x000000, 100);
		_root["grid"].moveTo(dotA._x, dotA._y);
		_root["grid"].lineTo(dotB._x, dotB._y);
	}
}

but i’m not sure how to turn back the dotArray, which is coordinates in sets of four, back into dotAx! The solution eludes me at the moment. THink you could help me out? thanx reuben

Actually… You are right… I was a bit tired when I posted that…

What I was referring to in the lines and creating a blank movieClip is just good practice. Of course you can use the _root median to draw all of your lines, but I wouldn’t suggest it.

I merely forgot to add 2 lines of code to my statement above, I apologize for the typo… it should look like this:


_root.onLoad =function()
{
   //initial
   dotA._x = 50;
   dotA._y = 50;
   dotB._x = 100;
   dotB._y = 100;
   //drawing
   _root.createEmptyMovieClip("line1", 100);
   with(_root.line1)
   {
      lineStyle(1, 0xFF0000);
      moveTo(dotA._x, dotA._y);
      lineTo(dotB._x, dotB._y);
   }
}

This “with” statement will allow you to target a set amount of code with a cerain object… It makes your life alot easier than typing out all those lines or copying and pasting as well… Plus it makes for better coding practices and neater code :slight_smile:

Have a good one man.

PS : With your problema above… It might be easier to just make four arguments in the jointhedots function than to concat and split the actual array itself… Trust me, it’ll be quicker and easier to handle in the long run that way :slight_smile:

Yeah i agree with your with statements, i usually use them to tidy it all up in the end. But i did try it with my first fla and it didn’t work, but it does now?!?

Anyway i’m not sure what u mean by

It might be easier to just make four arguments
? is an argument an array?

i thought i would do this instead, so instead of having one big array having lots of little ones (with all four points)

so when you press space it is now:

lineArray(dotBx, dotBy, dotAx, dotAy, noOfTurns);
 noOfTurns += 1;
 }

then the lineArray function:

function lineArray(pointBx, pointBy, pointAx, pointAy, arraynumber){
 _root["line"+arraynumber] = new Array();
 _root["line"+arraynumber][0] = pointBx;
 _root["line"+arraynumber][1] = pointBy;
 _root["line"+arraynumber][2] = pointAx;
 _root["line"+arraynumber][3] = pointAy;

and then at the end of the _root.onEnterFrame i call the joinTheDots function, then the joinTheDots function is:

function joinTheDots(){
 for(var i = 0; i<(noOfTurns+1); i++){
 _root["line"+i][0] = ptBx;
 _root["line"+i][1] = ptBy;
 _root["line"+i][2] = ptAx;
 _root["line"+i][3] = ptAy;
 _root.createEmptyMovieClip("grid"+i, 2000+1);
 _root["grid"+i].lineStyle(5, 0x000000);
 _root["grid"+i].moveTo(ptAx, ptAy);
 _root["grid"+i].lineTo(ptBx, ptBy);

or do u suggest something different, i can’t see a way of doing it without an array. BTW my method above doesn’t work, i’ve traced _root[“line”+arraynumber] and it tells me the correct array, but then i can’t get it to draw a black line. Questions, queries, problems?

BTW thanx Marz for helping out a noob when u clearly work hard, all i do is teach little kids to swim

Heh… You teach little kids to swim… I seel vitamins and websites… Meh… You probably have a helluva lot more fun than I do! :smiley:

But hey man… This is the structure of a function.

function functionName (argument1, argument2, …) {}

So when I stated arguments… I meant the variables that get passed inside of the parenthesis.

Now… As for your arrays… the ones you are referring to are theoretically not arrays… You are trying to add the array properties [0], [1], and so on into a movieClip declaration… to work with an array… You need to create a variable and give it the object type of an array.


   myArray = new Array();

Thne by using some of the statements and array caps, you can add onto arrays and splice arrays very simply… You can also access that array by calling it as myArray[0], myArray[1].

Some common array functions or subclass functions you might want to be aware of:

myArray.length : a property that returns the value of how many cells or how big the array is

myArray.push(values…) : allows you to add values onto the end of the array. Seperate each individual value wit a comma…

Example : myArray.push(“marz”, “c++”, “magick”);

These are just two possibilties… If you want to find out more about the Array class… Just look the Array class up in the ActionScript dictionary in your flash editor or online at macromedia.com.

Take it easy man.

I don’t know about more fun, i was nearly sued the other day when one little kid kicked another in the face, blood was going everwhere (mostly just because of the water, which makes it spread like crazy!). Anyway, yeah so i did know what an argument was just not what its called, i think thats true for a lot of flash things for me!
For an array putting someArray[0] = something; doesn’t that set the first element in that array to ‘something’? That’s what i always thought.

Ok i managed to get it to put down black lines in places, just cos of a stupid error: i put in the join the dots function:


 _root["line"+i][0] = ptBx;
  _root["line"+i][1] = ptBy;
  _root["line"+i][2] = ptAx;
  _root["line"+i][3] = ptAy;

instead of: <code>[color=#000000][color=black]ptBx = root[“line”+i][0]; etc and also forgot the line ptA = root["t"+(ptAx)+""+ptAy]; but that works know. (i was wondering why the array kept becoming “undefined”!)
1st Request: Ok my next problem (yep they just keep coming) is that after i press space twice my red line seems to go behind the dots, which is weird cause i don’t have any swapDepths anywhere! So i thought maybe it changed its depth to another objects, but that would make it disappear, not change depths. So it might be because i’m createEmptyMovieClip every frame, is this a problem, because i can put it as initial, but then my clear() doesn’t work! So i’m not sure about that, but its not absolutely essential, i’m just curious. (incase anyof this didn’t make sense check my fla)

2nd Request: if i have a grid of three numbers by 3, going 1-9, so no.1 is row1, col1, 2is row1,col2 etc, how would i find out which row and column a number is (remember the column width and row width are known), eg i know no.9 is row3, col3 but flash doesn’t, what’s the maths formula for this, i can’t seem to be able to get it right, because 7,8,9 would have to be the same row number?!?

Thanks for ur help so far marz, ur a gun
[/color][/color]</code>

Don’t need to worry about the 2nd request in last post got it all figured out, took me quite a bit of trial and error but i finally got it out, my attached fla has an engine for any nx*n *grid where u can check which square a line is attached to and what side, which will come in handy when implementing the ai.
I’m one step closer!

I won’t have a chance to reply heavily into this for the rest of the day / night… I apologize… But always check how your depths are being placed and make sure that the spacebar doesn’t change movieClips… If you say you are doing createEmptyMovieClips alot… Try and find another route around that… Making them invisible instead of moving or whatever… this is all I ca supply for now. Will elaborate more on this later sometime.

Thanks for that, i’m space bar doesn’t change depths (well shouldn’t anyway) and i think i’ve checked that depths are all correct, anyway its not really my major concern at the moment, got square detection out! yay!

For some reason that line going behind dots thing is no longer a problem (don’t ask me) i just checked my fla today when i was making it detect squares and i noticed it didn’t happen anymore. And now i’ve got the squares to colour in pretty colours when there surrounded, so i’ve nearly made a two player (human though) game, once i get that i’ll try the human vs computer da da dum, i’ve got 15 hours of exams this week which is kindof slowing down progress :frowning:

Yay i’ve managed to get this game working for a nxn grid for human vs human, and i think i’ve got rid off all the bugs, i’ll start with the ai on the weekend!

Hmm… I would suggest turning into a mouseControlled game though… Might be a little more associated and would help out alot…

Plus… After you place a vertical line… it wasn’t allowing me to spin any of the other lines around at all.

Yeah ‘shift’ turns it around one way and ‘end’ the other, they’re right next to each other on my laptop keyboard, i could let it be press shift once vertical twice horizontal at 12fps because it was to hard to get it to stop on the right one. As for mouse controlled, i hate using the mouse thing on the laptop (that little square mofo) and with the keyboard it ensures that the line is only one unit long, and not diagonal

Yeah but the majority of people playing would be from desktop… and I use a laptop also… But I carry a mouse with me at all times also… Because I hate the touchpads as well… They are pretty screwed up. :slight_smile:

Umm… Make it one key only… To rotate it around… Would be more simplistic.

Ok, i’ll make it one button press to turn around, i guess they’re dumb ppl out there who might get confused (well gotta think of everyone). ur the man marz.

BTW with my AI, i’ve got it to scan for squares that have three sides, then put one on the fourth side, but if there aren’t any it goes to the next empty line. Obviously this is pretty predictable, but i can’t use Math.random()*32 and then get it to choose a line, because if theyre say 32 lines they’res a 1/32 chance it will get a line, which can take a few secs to figure out.

Is there a way to store remaining lines in an array, find one randomly and then select it then remove it from the array. from the array methods in the flash dictionary i can’t see how this would be possible.

My sort of comprimise is to choose one at random, and if that one’s taken just go for next available one, because then when there’s only 1 line left, it won’t take 10 mins to find.

Do this… Have all the lines in an array…

lineStorage = new Array();

And then… Use this property lineStore.length;

It will give you the length of the current array…

The values in the array should be 1 - 32 or however many you want to place in it… I’d suggest using a for loop for this…


for(i=0; i<maxLines; i++)
{
  lineStorage* = i;
}

Now… When dealing with taking out numbers… You would have to use the splice function… This is a weird function… hehe

randomLine = Math.floor(Math.random()*lineStorage.length);
lingStorage.splice(randomLine, 1);