Thing is, i'd like to control the ball by pressing on screen keys (up, down, left, right) instead of using the mouse position as this will be appearing on a touch screen without a keyboard or mouse.
How do I go about doing this?
I've attached my .fla so show what I mean.
// (load) just sets up the movement boundaries, it is optional onClipEvent (load) { var left:Number = 0; var right:Number = Stage.width - this._width; var top:Number = 0; var bot:Number = Stage.height - this._height; }
onClipEvent (enterFrame) { if (Key.isDown(Key.UP)) { if (this._y > top) { this._y -= 5; } } if (Key.isDown(Key.DOWN)) { if (this._y < bot) { this._y += 5; } } if (Key.isDown(Key.LEFT)) { if (this._x > left) { this._x -= 5; } } if (Key.isDown(Key.RIGHT)) { if (this._x < right) { this._x += 5; } } }
I am not entirely sure if AS2 will work with touch screen.
thanks for that, really appreciate the help!
Unfortunately its not quite what i'm after, the script you wrote uses key presses, i'd managed to get that working, the trouble is there wont be a keyboard.
I dont think i explained it clearly enough, I need it to work with on screen button instances used to control the up, down, left and right movement of the ball.
The good news is AS2 does work with the touch screens we're using, they simply replace the x y coordinates of the mouse with those of the users finger on the screen!
with your newer method I don't think you will be able to do the easing as you are not giving it a place to ease to.
All you are doing is telling it to move a certain amount continually until you let go of the button.
I believe you would need a destination point for you to ease to and from.
You might find it easier to try and keep your code off of the buttons and clips so you can get to it at a glance (no trying to find code, all seeable)
you could use switch method too, you would need to name your buttons
PHP Code:
var buttonArray:Array = new Array(leftButton, rightButton, upButton, downButton);
var directions:Array = new Array("left", "right", "up", "down");
var moving:String;
You're right of course,
I was thinking of simulating easing by slowing down the speed at which the ball moves once you release the button until its speed = 0.
Although this too is proving troublesome!
You're right, you need a speed variable, actually two of them which keep track of the X and Y speed increment of your movieclip. This way, you can keep increasing your ball's X and Y coordinates by those two speed variables. Here, I fixed your file in accordance with your current code (although, I believe this could been in a way better method, but I don't know how much AS2 you know ) -- hopefully this is what you wanted?
If you need help understanding any part of the code, feel free to ask and I'll give you an explanation Also, your overuse of enterFrame events cause a few "bugs" with the ball's movement (like, if you press and hold UP, and then let go and press and hold RIGHT, the ball will start moving diagonal and never really "recover" from the UP movement) -- if you want to fix this, the code will have to be rewritten from scratch, so just ask and we'll try our best to help you