Ok, I have a script that I use for wall colisions. Here it is.
onClipEvent(load){
walkingState = "down";
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
gotoAndStop ("right");
walkingState = "right";
}
else if (Key.isDown(Key.LEFT)) {
gotoAndStop ("left");
walkingState = "left";
}
else if (Key.isDown(Key.DOWN)) {
gotoAndStop ("down");
walkingState = "down";
}
else if (Key.isDown(Key.UP))
{
gotoAndStop ("up");
walkingState = "up";
}
else{
gotoAndPlay(walkingState);
walkingState = "";
}
}
onClipEvent(load){
moveSpeed=10;
}
//start of wall script
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
if(_root.Walls.hitTest(getBounds(_root).xMax,_y,true)){
}else{
this._x += moveSpeed;
}
}else if(Key.isDown(Key.UP)){
if(_root.Walls.hitTest(_x,getBounds(_root).yMin,_y,true)){
}else{
this._y -= moveSpeed;
}
}else if(Key.isDown(Key.DOWN)){
if(_root.Walls.hitTest(_x,getBounds(_root).yMax,true)){
}else{
this._y += moveSpeed;
}
}else if(Key.isDown(Key.LEFT)){
if(_root.Walls.hitTest(getBounds(_root).xMin,_y,true)){
}else{
this._x -= moveSpeed;
}
}
}
Simple enough. But it works on frames, but when it switchs to a new scene it messes up. Many people have attempted to fix it and no one has. So while I work on it I borrowed another script from a site, and modified it a bit.
It allows the player to walk in any direction, but only faces 4. So its like old school games. Only prob is I dont know where to put in a script for collisions. Using another script that i’m not familiar with is kinda confusing. So anyone who can adapt well to scripting, help a fellow out. Below is the code.
// Variables
// character contains 16 frames, the first 8 of "standing"
// views in each 8 directions of movement, the following 8
// for the animated versions of those directions as it moves
character.gotoAndStop(1); // starting frame of character
character.move = {x:0, y:0}; // directions of movement
character.speed = 5; // speed of movement
// Events
character.onEnterFrame = function(){ // control movement
// get movement from key controls
this.move.x = Key.isDown(Key.RIGHT) - Key.isDown(Key.LEFT); // -1, 0 or 1
this.move.y = Key.isDown(Key.DOWN) - Key.isDown(Key.UP); // -1, 0 or 1
if (this.move.x || this.move.y){ // if being moved in any direction
// find frame, start at least on frame 9 (where movement clips are)
// then find an angle from the keys being pressed (Math.atan2), divide this by
// 8 directions (Math.PI/4) and make sure the value remains between 0-7 (%8)
var frame = 5 + Math.floor((Math.PI + Math.atan2(this.move.y, this.move.x))/(Math.PI/2))%4;
if (this._currentframe != frame) this.gotoAndStop(frame); // go to frame if not there already
// apply movement using movement by factor of speed
this._x += this.move.x * this.speed;
this._y += this.move.y * this.speed;
// * note that diagonal movement covers more ground given this kind of movement with speed
}else if (this._currentframe > 4){ // if not being moved and on an animation frame
this.gotoAndStop(this._currentframe - 4); // step back to the standing version of that frame
}
}
Oh, by the way. Above there is a spacing in true for some reason. Don’t know why kirupa won’t fix this but oh well. Thought i’d let you know its not realy there.