PDA

View Full Version : Help! My tile system is buggy!



BuG56
November 22nd, 2006, 04:19 PM
Hey all, I'm trying to make my own code to handle one square moving around a 500x300 stage at 10px and be able to stop when it hits another square, but it's sort of buggy. Both the character and the block that I want him to stop at are 10x10 squares.

Code on the frame:


/*Plot is a function I use so that instead of using the actual 500x300 to
plug in x and y values I can use a 50x30 based on where the square
can move */
function PLOT(xory) {
newxory = xory/10;
return (newxory);
}
/* Here I declare two arrays that hold whatever spots are occupied by non-passible objects (ie. my wall) */
takenXslots = new Array();
takenYslots = new Array();
/* This is Kirupa's wonderfully useful Array searching function */
arraySearch = function (input, array) {
for (i=0; i<array.length; i++) {
if (array[i] == input) {
return 1;
}
}
return -1;
};



The code on my wall :


onClipEvent (load) {
with (this) {
_x = 40;
_y = 70;
xslot = _root.PLOT(this._x);
yslot = _root.PLOT(this._y);
}
_root.takenXslots.push(this.xslot);
_root.takenYslots.push(this.yslot);
}


And the code on my character:


onClipEvent (load) {
with (this) {
_x = 100;
_y = 200;
}
}
onClipEvent (enterFrame) {
/* These determine the x and y values of the next move using the plot function */
this.nextLEFT = new Array(_root.PLOT(this._x -= 10), _root.PLOT(this._y));
this.nextRIGHT = new Array(_root.PLOT(this._x += 10), _root.PLOT(this._y));
this.nextUP = new Array(_root.PLOT(this._x), _root.PLOT(this._y += 10));
this.nextDOWN = new Array(_root.PLOT(this._x), _root.PLOT(this._y -= 10));
//MOVEMENT LEFT
if (Key.isDown(Key.LEFT)) {
if (_root.arraySearch(this.nextLEFT[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextLEFT[0], _root.takenXslots) == -1) {
this._x -= 10;
}
}
if (_root.arraySearch(this.nextLEFT[1], _root.takenYslots) == 1) {
if (_root.arraySearch(this.nextLEFT[0], _root.takenXslots) == -1) {
this._x -= 10;
}
}
if (_root.arraySearch(this.nextLEFT[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextLEFT[0], _root.takenXslots) == 1) {
this._x -= 10;
}
}
}
//MOVEMENT RIGHT
if (Key.isDown(Key.RIGHT)) {
if (_root.arraySearch(this.nextRIGHT[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextRIGHT[0], _root.takenXslots) == -1) {
this._x += 10;
}
}
if (_root.arraySearch(this.nextRIGHT[1], _root.takenYslots) == 1) {
if (_root.arraySearch(this.nextRIGHT[0], _root.takenXslots) == -1) {
this._x += 10;
}
}
if (_root.arraySearch(this.nextRIGHT[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextRIGHT[0], _root.takenXslots) == 1) {
this._x += 10;
}
}
}
//MOVEMENT UP
if (Key.isDown(Key.UP)) {
if (_root.arraySearch(this.nextUP[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextUP[0], _root.takenXslots) == -1) {
this._y -= 10;
}
}
if (_root.arraySearch(this.nextUP[1], _root.takenYslots) == 1) {
if (_root.arraySearch(this.nextUP[0], _root.takenXslots) == -1) {
this._y -= 10;
}
}
if (_root.arraySearch(this.nextUP[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextUP[0], _root.takenXslots) == 1) {
this._y -= 10;
}
}
}
//MOVEMENT DOWN
if (Key.isDown(Key.DOWN)) {
if (_root.arraySearch(this.nextDOWN[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextDOWN[0], _root.takenXslots) == -1) {
this._y += 10;
}
}
if (_root.arraySearch(this.nextDOWN[1], _root.takenYslots) == 1) {
if (_root.arraySearch(this.nextDOWN[0], _root.takenXslots) == -1) {
this._y += 10;
}
}
if (_root.arraySearch(this.nextDOWN[1], _root.takenYslots) == -1) {
if (_root.arraySearch(this.nextDOWN[0], _root.takenXslots) == 1) {
this._y += 10;
}
}
}
}
/* Originally I was messing around with the Left movement and it seemed to work if i presented all three possible cases as shown (X and Y clear / X clear and Y is not, Y clear and X is not )*/


When I test the movie, the following happens :

Left movement works perfectly

Right movement stops when INSIDE the wall rather than one space to the left of it

When moving down it has a similar effect to the right (stopping inside instead of 1 space up)

When moving up it stops a space AHEAD of the square instead of two below it.

Whats wrong with my code?!?! :puzzle:

Any help would be great, thanks in advance.

bombsledder
November 22nd, 2006, 07:51 PM
i think you should re-write it to tell ya the truth and instead of placing your tiles in an array place it in an object, that way you can do it like this,



var tiles:Object={}
function placeTile(x:Number,y:Number):Void{
tiles["tile_"+x+"_"+"y"] = _root.attachMovie("mytile","tiles_"+x+"_"+y,_root.getNextHighestDepth(),{_x:x,_y:y})
}

function getTile(x:Number,y:Number):MovieClip{
return tiles["tile_"+x+"_"+"y"]
}


I think this might help a little bit in the future prolly not with your problem now but i will write a little engine for you and post it tommorw maybe

BuG56
November 25th, 2006, 05:36 AM
Thanks a lot, this is my second time rewriting this engine. I don't know too much about Objects, but I'll be sure to check it out. An engine would be really helpful. I would rather fix my own engine though so if anybody could tell me whats wrong with it.

bombsledder
November 25th, 2006, 11:24 AM
well one thing you should know about objects is that every class extends an object, and to loop through an object like you would an array you write it like this



for(var prop in myObject){
trace(prop)
}