I started learning ActionScript 3.0, I got some basic notions of programming from Pascal.
A brief explaining on why I'm gonna ask what I'm gonna ask: I don't believe in learning trough theory - example, I was watching a video on tricks you can do in PES 2013 (pro evolution soccer) and noticed I forgot them one by one as the video kept moving foward, only after using them ingame they ocurred to me naturally when needed. This goes for anything, everyone learns trough direct experience, a baby has no language, he doesn't need theory on walking nor on speaking, simply by being around others and try-fail method he learns to walk and communicate (one of the reasons I think we don't need to be taught portuguese grammar at school, but to read and talk instead).
I have this method of getting a code, reading, then erasing this and that line and see what happens. Pascal was pretty intuitive in the syntax, I learned a lot by erasing, changing lines and see what happened. After a bit of this only then I'd look for the theory, which would make sense at the mark in time and not be some random fact in my brain.
So I am here to ask a very simple code example ^_^" - Something like a square that moves left/right as you press the two arrow keys.
I am reading trough stuff regardless and looking for the tip I can grab and start pulling. I did watch a tutorial which seemed simple but he wasn't using Flash Develop which is the one I have access to, and so I didn't have the tools to draw a square as he did in the tutorial, plus other stuff. (Creating a 'Copter' style game in Flash - ActionScript 3)
Code for moving square left and right ;) I did it simple, so you can study it.
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.ui.Keyboard;
public class StackOverflow extends Sprite {
private var _keyListener:KeyListener;
private var _actor:Actor;
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
start();
}
private function start():void {
_actor = new Actor(40, 0x323232);
_keyListener = new KeyListener(stage);
addChild(_actor);
_actor.x = stage.stageWidth * 0.5;
_actor.y = stage.stageHeight * 0.5;
//Main loop
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
if (_keyListener.isDown(Keyboard.RIGHT)) {
_actor.x += _actor.speed;
}
if (_keyListener.isDown(Keyboard.LEFT)) {
_actor.x -= _actor.speed;
}
}
}
}
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
internal class KeyListener {
private var _keys:Object;
public function KeyListener(stage:Stage) {
_keys = {};
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
stage.addEventListener(Event.DEACTIVATE, onDeactivate);
}
public function isDown($keyCode:uint):Boolean {
return ($keyCode in _keys);
}
private function onDeactivate(e:Event):void {
_keys = {};
}
private function onKeyPressed(e:KeyboardEvent):void {
_keys[e.keyCode] = true;
}
private function onKeyReleased(e:KeyboardEvent):void {
if (e.keyCode in _keys) {
delete _keys[e.keyCode];
}
}
}
internal class Actor extends Sprite {
private var _speed:Number;
public function Actor(size:uint, color:uint, speed:Number = 4) {
_speed = speed;
this.graphics.beginFill(color);
this.graphics.drawRect(-size * 0.5, -size * 0.5, size, size);
}
public function get speed():Number {
return _speed;
}
}