flash actionscript:
Here's the current actionscript i have: // first set up spacing and collisions spacing = 20; movingTo = new Array(); collisions = [ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]; // set up a function to move the ball to a new grid x,y // then set the new x, y to the ball's x, y variables moveToGridSpace = function(x1,y1){ movingTo.push({x:spacing * (x1 + y1),y:spacing/2 * (x1 - y1)}); ball.x = x1; ball.y = y1; } setInterval(moveBall,1,ball); function moveBall(someBall){ if(movingTo.length>0){ cmoveTo = movingTo[0]; moveOnce(ball,cmoveTo.x,cmoveTo.y); } } function moveOnce(someBall,x1,y1){ x = x1-someBall._x; y = y1-someBall._y; angle = Math.atan(y/x)/(Math.PI/180); if (x<0)angle += 180; if (x>=0 && y<0)angle += 360; degs = angle; rads = degs/180*Math.PI; step = .8; isoSin = Math.sin(rads); isoCos = Math.cos(rads); stepY = isoSin*step; stepX = isoCos*step; someBall._x+=stepX*step someBall._y+=stepY*step if(Math.abs(x)<Math.abs(isoCos*step) && Math.abs(y)<Math.abs(isoSin*step) || someBall._x==x1 && someBall._y==y1){ someBall._x = x1; someBall._y = y1; movingTo.shift() } updateAfterEvent() } // set up a function to get the ball's grid x,y based on its position // then assign the new values to the ball's x, y variables SetGridSpace = function(){ ball.x = Math.round((ball._x/spacing + 2*ball._y/spacing)/2); ball.y = Math.round((ball._x/spacing - 2*ball._y/spacing)/2); } // set a function to determine if the grid space x, y is a valid // space for movement returning true if valid, false if not IsValidTile = function(x,y){ if (collisions[x][y] === 0) return true; else return false; } // set the ball to recognize key presses and move based on them Key.addListener(ball); ball.onKeyDown = function(){ var xmove, ymove; if (Key.isDown(Key.UP)){ ymove = 1; }else if (Key.isDown(Key.DOWN)){ ymove = -1; }else if (Key.isDown(Key.LEFT)){ xmove = -1; }else if (Key.isDown(Key.RIGHT)){ xmove = 1; } // update new position based on ball's current xmove += this.x; ymove += this.y; if (IsValidTile(xmove, ymove)){ moveToGridSpace(xmove, ymove); } } // additionally we can set the ball to be put where the mouse // was pressed on the screen. ball.onMouseDown = function(){ var mouse_x = Math.round((_xmouse/spacing + 2*_ymouse/spacing)/2); var mouse_y = Math.round((_xmouse/spacing - 2*_ymouse/spacing)/2); if (IsValidTile(mouse_x, mouse_y)){ moveToGridSpace(mouse_x, mouse_y); } } // update the ball x, y position based on where it was placed // initially on the screen SetGridSpace(); // then move the ball to be exact on that grid space MoveToGridSpace(ball.x, ball.y); It basically means that in the array grid that 0=a walkable tile and 1=a non walkable tile. Can i set the array so that say 2=goto next scene and 3 triggers something else etc? What would the script be for 2=goto next scene? Thanks
Actually, it is a lot more complicated than it sounds! Not sure if this revised code will work... Change the stepActions function to however you want. In that function, steppedOn will represent the number the character stepped on. Also, don't forget to change the collisions array, and add some 2's and 3's! function stepAction(steppedOn){ if(steppedOn==0){ trace('You stepped on a normal square!'); }else if(steppedOn==2){ trace('Oh no! You stepped on a 2!'); }else if(steppedOn==3){ trace('You stepped on a 3!'); } } // first set up spacing and collisions spacing = 20; movingTo = new Array(); collisions = [ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]; // set up a function to move the ball to a new grid x,y // then set the new x, y to the ball's x, y variables moveToGridSpace = function(x1,y1){ movingTo.push({x:spacing * (x1 + y1),y:spacing/2 * (x1 - y1),xs:x1,ys:y1}); ball.x = x1; ball.y = y1; } setInterval(moveBall,1,ball); function moveBall(someBall){ if(movingTo.length>0){ cmoveTo = movingTo[0]; moveOnce(ball,cmoveTo); } } function moveOnce(someBall,ballProps){ x1 = ballProps.x; y1 = ballProps.y; x = x1-someBall._x; y = y1-someBall._y; angle = Math.atan(y/x)/(Math.PI/180); if (x<0)angle += 180; if (x>=0 && y<0)angle += 360; degs = angle; rads = degs/180*Math.PI; step = .8; isoSin = Math.sin(rads); isoCos = Math.cos(rads); stepY = isoSin*step; stepX = isoCos*step; someBall._x+=stepX*step someBall._y+=stepY*step if(Math.abs(x)<Math.abs(isoCos*step) && Math.abs(y)<Math.abs(isoSin*step) || someBall._x==x1 && someBall._y==y1){ stepAction(collisions[ballProps.xs][ballProps.ys]); someBall._x = x1; someBall._y = y1; movingTo.shift() } updateAfterEvent() } // set up a function to get the ball's grid x,y based on its position // then assign the new values to the ball's x, y variables SetGridSpace = function(){ ball.x = Math.round((ball._x/spacing + 2*ball._y/spacing)/2); ball.y = Math.round((ball._x/spacing - 2*ball._y/spacing)/2); } // set a function to determine if the grid space x, y is a valid // space for movement returning true if valid, false if not IsValidTile = function(x,y){ if (collisions[x][y] !== 1) return true; else return false; } // set the ball to recognize key presses and move based on them Key.addListener(ball); ball.onKeyDown = function(){ var xmove, ymove; if (Key.isDown(Key.UP)){ ymove = 1; }else if (Key.isDown(Key.DOWN)){ ymove = -1; }else if (Key.isDown(Key.LEFT)){ xmove = -1; }else if (Key.isDown(Key.RIGHT)){ xmove = 1; } // update new position based on ball's current xmove += this.x; ymove += this.y; if (IsValidTile(xmove, ymove)){ moveToGridSpace(xmove, ymove); } } // additionally we can set the ball to be put where the mouse // was pressed on the screen. ball.onMouseDown = function(){ var mouse_x = Math.round((_xmouse/spacing + 2*_ymouse/spacing)/2); var mouse_y = Math.round((_xmouse/spacing - 2*_ymouse/spacing)/2); if (IsValidTile(mouse_x, mouse_y)){ moveToGridSpace(mouse_x, mouse_y); } } // update the ball x, y position based on where it was placed // initially on the screen SetGridSpace(); // then move the ball to be exact on that grid space MoveToGridSpace(ball.x, ball.y);
Cool, that works great. So if i wanted it to goto the next scene i have to replace the trace section? What script shall i put for it to goto scene 2 when its on tile 2?
Sorry, the script i posted first was an old version, i have a new version with this script: // first set up spacing and collisions spacing = 20; movingTo = new Array(); collisions = [ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]; // set up a function to move the ball to a new grid x,y // then set the new x, y to the ball's x, y variables moveToGridSpace = function(x1,y1){ movingTo.push({x:spacing * (x1 + y1),y:spacing/2 * (x1 - y1)}); ball.x = x1; ball.y = y1; } setInterval(moveBall,1,ball); function moveBall(someBall){ if(movingTo.length>0){ cmoveTo = movingTo[0]; moveOnce(ball,cmoveTo.x,cmoveTo.y); } } function moveOnce(someBall,x1,y1){ x = x1-someBall._x; y = y1-someBall._y; angle = Math.atan(y/x)/(Math.PI/180); if (x<0)angle += 180; if (x>=0 && y<0)angle += 360; degs = angle; rads = degs/180*Math.PI; step = .8; isoSin = Math.sin(rads); isoCos = Math.cos(rads); stepY = isoSin*step; stepX = isoCos*step; someBall._x+=stepX*step someBall._y+=stepY*step if(Math.abs(x)<Math.abs(isoCos*step) && Math.abs(y)<Math.abs(isoSin*step) || someBall._x==x1 && someBall._y==y1){ someBall._x = x1; someBall._y = y1; movingTo.shift() } updateAfterEvent() } // set up a function to get the ball's grid x,y based on its position // then assign the new values to the ball's x, y variables SetGridSpace = function(){ ball.x = Math.round((ball._x/spacing + 2*ball._y/spacing)/2); ball.y = Math.round((ball._x/spacing - 2*ball._y/spacing)/2); } // set a function to determine if the grid space x, y is a valid // space for movement returning true if valid, false if not IsValidTile = function(x,y){ if (collisions[x][y] === 0) return true; else return false; } // set the ball to recognize key presses and move based on them Key.addListener(ball); released = new Boolean(true); ball.onKeyDown = function(){ if(released){ released = false; var xmove, ymove; if (Key.isDown(Key.UP)){ ymove = 1; }else if (Key.isDown(Key.DOWN)){ ymove = -1; }else if (Key.isDown(Key.LEFT)){ xmove = -1; }else if (Key.isDown(Key.RIGHT)){ xmove = 1; } // update new position based on ball's current xmove += this.x; ymove += this.y; if (IsValidTile(xmove, ymove)){ moveToGridSpace(xmove, ymove); } } } ball.onKeyUp = function(){ released = true; } When i pasted the script in that you sent back the character wasnt constraining to the area set in the array. I tried just pasting in the script before the array section and the character wouldnt walk on tile's with 2 or 3. The goto scene 2 question still applies aswell.
I changed a few other things in the code, so it won't work. Try this: First, select Frame 1 of Scene 2, open the properties panel, and type in scene2_1 as the <Frame Label> Use this code: function stepAction(steppedOn){ if(steppedOn==0){ trace('You stepped on a normal square!'); }else if(steppedOn==2){ trace('Oh no! You stepped on a 2!'); _root.gotoAndStop("scene2_1"); }else if(steppedOn==3){ trace('You stepped on a 3!'); } } // first set up spacing and collisions spacing = 20; movingTo = new Array(); collisions = [ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]; // set up a function to move the ball to a new grid x,y // then set the new x, y to the ball's x, y variables moveToGridSpace = function(x1,y1){ movingTo.push({x:spacing * (x1 + y1),y:spacing/2 * (x1 - y1),xs:x1,ys:y1}); ball.x = x1; ball.y = y1; } setInterval(moveBall,1,ball); function moveBall(someBall){ if(movingTo.length>0){ cmoveTo = movingTo[0]; moveOnce(ball,cmoveTo); } } function moveOnce(someBall,ballProps){ x1 = ballProps.x; y1 = ballProps.y; x = x1-someBall._x; y = y1-someBall._y; angle = Math.atan(y/x)/(Math.PI/180); if (x<0)angle += 180; if (x>=0 && y<0)angle += 360; degs = angle; rads = degs/180*Math.PI; step = .8; isoSin = Math.sin(rads); isoCos = Math.cos(rads); stepY = isoSin*step; stepX = isoCos*step; someBall._x+=stepX*step someBall._y+=stepY*step if(Math.abs(x)<Math.abs(isoCos*step) && Math.abs(y)<Math.abs(isoSin*step) || someBall._x==x1 && someBall._y==y1){ stepAction(collisions[ballProps.xs][ballProps.ys]); someBall._x = x1; someBall._y = y1; movingTo.shift() } updateAfterEvent() } // set up a function to get the ball's grid x,y based on its position // then assign the new values to the ball's x, y variables SetGridSpace = function(){ ball.x = Math.round((ball._x/spacing + 2*ball._y/spacing)/2); ball.y = Math.round((ball._x/spacing - 2*ball._y/spacing)/2); } // set a function to determine if the grid space x, y is a valid // space for movement returning true if valid, false if not IsValidTile = function(x,y){ if (collisions[x][y] !== 1) return true; else return false; } // set the ball to recognize key presses and move based on them Key.addListener(ball); released = new Boolean(true); ball.onKeyDown = function(){ if(released){ released = false; var xmove, ymove; if (Key.isDown(Key.UP)){ ymove = 1; }else if (Key.isDown(Key.DOWN)){ ymove = -1; }else if (Key.isDown(Key.LEFT)){ xmove = -1; }else if (Key.isDown(Key.RIGHT)){ xmove = 1; } // update new position based on ball's current xmove += this.x; ymove += this.y; if (IsValidTile(xmove, ymove)){ moveToGridSpace(xmove, ymove); } } } ball.onKeyUp = function(){ released = true; }
The goto scene 2 works, least thats one problem out of the way. The character is still not constrained to the area, he was before the new script was put in. Could this have something to do with the array?
So now the character can go on 1 spaces? Hmmm... Try changing the isValidTile function to this: IsValidTile = function(x,y){ trace('You stepped on '+collisions[x][y]); if(collision[x][y] != 1){ trace('You aren't allowed to step on 1!'); } if (collisions[x][y] != 1) return true; else return false; } When you run the code, and try moving the character, what is traced? Then, if that doesn't work try this: IsValidTile = function(x,y){ if (collisions[x][y] == 1){ return false; }else return false; } }
My array grid is set up like this: http://www.iso-land.com/movementtest4.swf I made the array grid so it stays within the floor but with the new script thats been added tonight the character goes outside the floor. The character wont walk on the 1 tiles, he just walks everywhere else outside the main floor even though the grid shouldnt let him. I tried the script you just gave me but loads of errors come up with it. Thanks
Ahh... then this should work: IsValidTile = function(x,y){ if (collisions[x][y] !== 1 && collisions[x][y]!=undefined) return true; else return false;
The character doesn't move at all now, i pasted that script over: IsValidTile = function(x,y){ if (collisions[x][y] !== 1) return true; else return false;
How about: IsValidTile = function(x,y){ if ((collisions[x][y] !== 1) && (collisions[x][y]!=undefined)) return true; else return false; } If that doesn't work, can you try: IsValidTile = function(x,y){ trace(collisions[x][y]); if (collisions[x][y] !== 1) return true; else return false; } When you run this code, what is traced normally, and what is traced when you go outside the boundaries?
Yay, this one worked: IsValidTile = function(x,y){ if ((collisions[x][y] !== 1) && (collisions[x][y]!=undefined)) return true; else return false; } Is there any problem you cant solve!!! Thanks very much for the patience and help yet again!!
Don't see what you're looking for? Try a search.
|