00:00
00:00
Newgrounds Background Image Theme

velvzie just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

finding the player postion

460 Views | 3 Replies
New Topic Respond to this Topic

finding the player postion 2014-09-20 09:35:48


finding the palyer postion by dividing the tile width in a 2d array

I am currently using the method were divide the tle width by the player postion to acess the array. Read below to see why the method. Should i use another ethod to get more accurate results. The follwoing is psuedo code

The player postion is 50 pixels across the first game square .The tile is 100 pixels in width. 50 / 100 = 0.5 (halfway point )if i use the math. round on 0.5 . Which is the player postion divided by the tile width and then rounded up The value of 0.5 rounded up is 1 the array access operator moved across 1 postion in the first row. When the player is still only half way across the first tile . The array access operator has now moved across 1 postion index[1]. When the player is still in tile1 (index postion [0]. Dividing the player postion by the tile width and then rounding the number up( the math.round method). The array postion will access the next index(tile) square 50 pxels before the player . Diagram 1 below will demonstrate

see link here

this. http://fav.me/d7zucib


BBS Signature

Response to finding the player postion 2014-09-20 12:06:49


At 9/20/14 09:35 AM, 512Pixel wrote: Dividing the player postion by the tile width and then rounding the number up( the math.round method).

I don't know exactly what this post is intended to do? (Is this a question? There is no question.)
But normally you would use math.floor (round down), not math.round to calculate the array index.

Response to finding the player postion 2014-09-20 15:06:31


At 9/20/14 12:06 PM, Etherblood wrote:
At 9/20/14 09:35 AM, 512Pixel wrote: Dividing the player postion by the tile width and then rounding the number up( the math.round method).
I don't know exactly what this post is intended to do? (Is this a question? There is no question.)
But normally you would use math.floor (round down), not math.round to calculate the array index.

yes it is a question i will try math.floor. The reason for the post is to show were my error could be. The workings out show the method Ive used and show how this method is not working .As th issue will be in the collision detection later.
I could post the code but ive pseudo code as this makes it better for a solution ....

I will try math.floor see if that give me more accurate result thank you .


BBS Signature

Response to finding the player postion 2014-09-21 14:17:58


At 9/20/14 12:06 PM, Etherblood wrote:
At 9/20/14 09:35 AM, 512Pixel wrote: Dividing the player postion by the tile width and then rounding the number up( the math.round method).
I don't know exactly what this post is intended to do? (Is this a question? There is no question.)
But normally you would use math.floor (round down), not math.round to calculate the array index.

I have now placed the code below. I idn htink the code would help . but i do have an issue the player at some point will always be haflway between a tile. he palyer for exmple on tile1 andtile 2 . This now means i am unable to use a collision detection just by using the tiles in the arraay . As the palyer at some point is inbetween . The array is then either give one resultor the other . its a chioce between tile 1 or tile2.

does anyoneknow a solution to thiis would i set up a 4 corner collision detection
I have been looking and think sometimes set up a 4 point corner collision detection

var mapWidth = 5;
var mapHeight = 4;
var tileWidth = 100;
var tileHeight = 100;

var pets:Array = [
["bockStart","block1",1,0,],
["2nd row","bottblock2",1,0,],
[0,1,1,0,],
[0,0,0,1,1,]
];
for (var i:int=0; i<mapHeight; i++)
{
for (var u:int=0; u<mapWidth; u++){
var cell:MovieClip = new tile();
cell.gotoAndStop(pets[i][u]+1);
cell.x=tileWidth*u
cell.y=tileHeight*i
addChild(cell);
}

};

//based on 100 pixels

//box3.x=0;box3.y=0;
//box4.x=100;box4.y=0;
//

///_______________________________________________________________________________________________

var player2:MovieClip = new player();
addChild(player2);
player2.width = 100;
player2.height = 100;
var playerWidth = player2.width ;
trace("playerWidth = " + playerWidth );

var playerStart = player2.y= 50;
//trace(playerStart);
var speed =5;

//___________________________________________________________________________________________________

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{

var playerPosRight = player2.x + 50 ;
var colPos =playerPosRight/tileWidth ;
var playerPosY = player2.y + 50;
var rowPos:Number =playerPosY/tileWidth ;

if (e.keyCode == Keyboard.RIGHT){
player2.x += speed;
trace("player2.x" + player2.x );
trace("playerPosRight " +playerPosRight );

trace(pets[rowPos][colPos]);
if(pets[rowPos][colPos] ==1){
player2.x += -25;
trace("hit");
}
}

if (e.keyCode == Keyboard.LEFT){
player2.x += -speed;
}

if (e.keyCode == Keyboard.DOWN){
player2.y += speed;
trace(player2.y)
trace(playerPosY );
trace("colPos = " + colPos);
trace("rowPos = " + rowPos);
trace( "col pos and row" +pets[rowPos][colPos]);

if(pets[rowPos][colPos] ==1){
player2.y += -25;
trace("hit");
}
}

if (e.keyCode == Keyboard.UP){
player2.y += -speed;

}
}


BBS Signature