playh5 Posted October 10, 2014 Share Posted October 10, 2014 Hi, I have this problem which makes my head scratching for hours, so I added a sprite by a for loop so I have 4 tiles in row and 4 tiles in column. Then I store the positions in an array with elements as phaser points. This way I can record the exact original position for later use. Then I want the tiles to randomly select a new point in random but not its original point. I tried so many times, but I am stuck in having two tiles at the same point and sometimes I made a looping that does not position the tiles in random. Any help would be greatly appreciated. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
IamThatGuy Posted October 11, 2014 Share Posted October 11, 2014 I am not familiar with phaser, but it would work something like this//starting locationvar orig_x = 2;var orig_y = 2;//random new location 0-3. Arrays start at 0 so 4 len/widevar new_x = Math.floor(Math.random() * 3);var new_y = Math.floor(Math.random() * 3); //Keep on looping until the new x isn't 2 and new y isn't 2while(orig_x <> new_x && orig_y <> new_y){new_x = Math.floor(Math.random() * 3);new_y = Math.floor(Math.random() * 3);}I am assuming your positions are grid positions. If your poition is mouse co-ords. It will work a little different. Either way the same concept will work.Map[x][y] Quote Link to comment Share on other sites More sharing options...
playh5 Posted October 11, 2014 Author Share Posted October 11, 2014 Thanks! In as3 I did it like this although there is always a tile that is going to its original x and y position.import flash.geom.Point;var sArray = [a,b,c,d,e,f,g,h];var pointList = [];var newPointList = [];// store mc original positions into arrayfor(var i = 0;i < sArray.length; i++){ var point:Point = new Point(sArray[i].x,sArray[i].y); pointList.push(point);}trace(pointList);while(pointList.length > 0){ var r:int = Math.floor(Math.random()*pointList.length); newPointList.push(pointList[r]); pointList.splice(r,1);}for(var n = 0;n < sArray.length; n++){ var newPoint:Point = newPointList[n]; sArray[n].x = newPoint.x; sArray[n].y = newPoint.y;} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.