PDA

View Full Version : Tile Based wall hitTest



DangerousDan
April 25th, 2006, 08:11 PM
I'm trying to make it so my character (named guy) can't walk through walls but I can't get the wall's x value. this["tile"+i]._x comes up as undefined when put through a trace. How do I check if he's hitting the walls? I've searched through the tutorials and I'm pretty sure this will be a easy quick fix up.

Heres my code:

map1 = [[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1]];
function placeTile(x,y,type,count){
if(type==1){
this.attachMovie("block", "tile"+c,c);
this["tile"+c]._x = x+this["tile"+c]._width/2;
this["tile"+c]._y = y+this["tile"+c]._height/2;
}
}
c = 0;
for(i = 0; i <= 10; i++){
for(j = 0; j <= 10; j++){
placeTile(j*20,i*20,map1[i][j],c);
c++;
}
}
this.attachMovie("guy","guy",1);
guy._x=1;
speed = 5;
function checkRight(x,y){
for(i = 0; i <= 100; i++){
if(this["tile"+i].hitTest(x,y,true)){
trace(this["tile"+i]._x);
return false;
}
else{
trace(this["tile"+i]._x);
trace("Not Touching...");
return true;
}
}
}
guy.onEnterFrame = function(){
if(Key.isDown(Key.RIGHT) && checkRight(guy._x+speed,guy._y)==true){
guy._x+=speed;
}
}
if you guys still need FLA (I doubt it...) I will provide it.

Nich
April 25th, 2006, 11:03 PM
hmm... For starters, in here:



function placeTile(x,y,type,count){
if(type==1){
this.attachMovie("block", "tile"+c,c);
this["tile"+c]._x = x+this["tile"+c]._width/2;
this["tile"+c]._y = y+this["tile"+c]._height/2;
}
}

you don't define c, just count. That might be your problem right there

DangerousDan
April 26th, 2006, 12:29 PM
hmm... For starters, in here:


ActionScript Code:


</p>
<p>function placeTile(x,y,type,count){</p>
<p> if(type==1){</p>
<p> this.attachMovie("block", "tile"+c,c);</p>
<p> this["tile"+c]._x = x+this["tile"+c]._width/2;</p>
<p> this["tile"+c]._y = y+this["tile"+c]._height/2;</p>
<p> }</p>
<p>} </p>
<p>






you don't define c, just count. That might be your problem right there
Thats not it, but that is a mistake. C is increasing each time so it is just replacing count. I could just delete count now ^_^*, thanks for the reply but is more the hitTest that I'm having trouble with.

JoshuaJonah
April 26th, 2006, 01:04 PM
map1 = [[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1]];
function placeTile(x,y,type,count){
if(type==1){
var thetile:MovieClip = this.attachMovie("block", "tile"+c,c);
thetile._x = x+this["tile"+c]._width/2;
thetile._y = y+this["tile"+c]._height/2;
}
}
c = 0;
for(i = 0; i <= 10; i++){
for(j = 0; j <= 10; j++){
placeTile(j*20,i*20,map1[i][j],c);
c++;
}
}
this.attachMovie("guy","guy",1);
guy._x=1;
speed = 5;
function checkRight(x,y,selected_tile){
for(i = 0; i <= 100; i++){
if(selected_tile.hitTest(x,y,true)){
trace(selected_tile._x);
return false;
}
else{
trace(selected_tile._x);
trace("Not Touching...");
return true;
}
}
}
guy.onEnterFrame = function(){
if(Key.isDown(Key.RIGHT) && checkRight(guy._x+speed,guy._y, this)==true){
guy._x+=speed;
}
}
?

DangerousDan
April 26th, 2006, 01:08 PM
I'll have to try this when I get home thanks man.:A+:

DangerousDan
April 26th, 2006, 06:03 PM
I think selected_tile is refering to guy when used with the command this.

if(Key.isDown(Key.RIGHT) && checkRight(guy._x+speed,guy._y, this)==true)
When you said this I think it means the guy's x rather then the wanted tile's x. Regardless the selected_tile's x is tracing out as 1, the guy's x. I thank you for your attempt I'll give this another whack tomorrow.

DangerousDan
April 27th, 2006, 10:28 AM
UPDATE!
Tile-Hittesting works for the most part, I'm having trouble deciding when/where to put the guy._x-guy._width/2. Check out that section of the code.

map1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
function placeTile(x, y, type, count) {
if (type == 1) {
this.attachMovie("block", "tile"+c, c);
this["tile"+c]._x = x+this["tile"+c]._width/2;
this["tile"+c]._y = y+this["tile"+c]._height/2;
}
}
c = 0;
for (i=0; i<=10; i++) {
for (j=0; j<=10; j++) {
placeTile(j*20, i*20, map1[i][j], c);
c++;
}
}
this.attachMovie("guy", "guy", 1);
guy._x = 1;
guy._y = 1 + guy._height/2;
speed = 5;
m=0;
function checkTile(x, y) {
for(m=0;m<=100;m++) {
if (this["tile"+m].hitTest(x, y, true)) {
trace("Touching: "+this["tile"+m]._y+" x:"+x+" Guy is "+y+" X:"+x );
return false;
}
}
return true;
}
onEnterFrame = function() {
if (Key.isDown(Key.RIGHT) && checkTile(guy._x+speed+guy._width/2, guy._y) == true && guy._x < 200-guy._width/2) {
guy._x += speed;
}
if (Key.isDown(Key.LEFT) && checkTile(guy._x-speed-guy._width/2, guy._y) == true && guy._x > 0+speed+guy._width/2) {
guy._x -= speed;
}
if (Key.isDown(Key.DOWN) && checkTile(guy._x+guy._width/2, (guy._y-speed)+guy._height/2) == true && guy._y < 200-guy._width/2) {
guy._y += speed;
}
if (Key.isDown(Key.UP) && checkTile(guy._x-guy._width/2, guy._y-speed-guy._height/2) == true && guy._y > 0+speed+guy._height/2) {
guy._y -= speed;
}
};

Theres the whole code, heres the part that I'm messing up on.

function checkTile(x, y) {
for(m=0;m<=100;m++) {
if (this["tile"+m].hitTest(x, y, true)) {
trace("Touching: "+this["tile"+m]._y+" x:"+x+" Guy is "+y+" X:"+x );
return false;
}
}
return true;
}
onEnterFrame = function() {
if (Key.isDown(Key.RIGHT) && checkTile(guy._x+speed+guy._width/2, guy._y) == true && guy._x < 200-guy._width/2) {
guy._x += speed;
}
if (Key.isDown(Key.LEFT) && checkTile(guy._x-speed-guy._width/2, guy._y) == true && guy._x > 0+speed+guy._width/2) {
guy._x -= speed;
}
if (Key.isDown(Key.DOWN) && checkTile(guy._x+guy._width/2, (guy._y-speed)+guy._height/2) == true && guy._y < 200-guy._width/2) {
guy._y += speed;
}
if (Key.isDown(Key.UP) && checkTile(guy._x-guy._width/2, guy._y-speed-guy._height/2) == true && guy._y > 0+speed+guy._height/2) {
guy._y -= speed;
}
};
I'll send a FLA too, hope this helps (-:

Nich
April 27th, 2006, 08:07 PM
Put in a "else if(checkTile(guy._x+speed+guy._width/2, guy._y) == false)" after each direction, and put your code in that