I'm scripting a top-down game here, and it's pretty much my first major Flash project and I'm sort of stuck. My AS appears to be all sound, so I can't figure out why this doesn't work.
Below is the code that is on my player character.
ActionScript Code:
// The 'onClipEvent' header is to make sure everything is executed on each frame so that the game always works
onClipEvent(enterFrame){
// Pointing script for the object
this.pointAt(this._parent._xmouse, this._parent._ymouse);
// Movement keys
if (Key.isDown( 65 )) {
this._x = this._x - movement;
}
if (Key.isDown( 87 )) {
this._y = this._y + movement;
}
if (Key.isDown( 83 )) {
this._y = this._y - movement;
}
if (Key.isDown( 68 )) {
this._x = this._y + movement;
}
// End movement keys
}
This is the code that defines variables and makes the pointAt script possible, or maybe it's unnecessary...
ActionScript Code:
// Point at cursor script
MovieClip.prototype.pointAt = function(x,y)
{
var dx = x - this._x; // distance to other object on x-axis
var dy = y - this._y; // distance on y-axis
var dist = Math.sqrt(dx*dx + dy*dy); // true distance (using Pythagorean theorem)
var angle;
// figure out angle in radians (0- 2*PI)
if (dy <0)
angle = Math.PI*2-Math.acos(dx/dist);
else
angle = Math.acos(dx/dist);
// convert to rotation value (angle in degrees or 0-360)
this._rotation = angle*180/Math.PI;
}
// End point at cursor script
//----------------------------------------------------------------------------------------------
// Variables
// Player Variables
movement=1
// Enviromental Variables (Wind, Gravity, Drag, etc.)
FrisbeeSpeed=8
gravity=0.7
drag=0.8
wind=0.4
And of course, the code for my crosshair
ActionScript Code:
onClipEvent (enterFrame) {
this._x = _root._xmouse;
this._y = _root._ymouse;
Mouse.hide();
}
As you can see, I tend to document my code pretty well so others can use it if they need to. But what my problem is is that when I press D - No other key, just D - my player movie clip teleports to the center of the movie and won't move. My other controls don't work either. Is there something missing here? Or is my coding just wrong? Please help!