Archive | Flash tuts RSS feed for this section

Moving stuff with arrow keys and WASD.

23 Jul

I made a little drawing called ‘Ship’. The following code lets you move ‘Ship’ left and right using the arrow keys and the keys ‘A’ and ‘D’.

package {
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import flash.ui.Keyboard;
	public class Engine extends MovieClip {
		var ourShip:Ship = new Ship();
		var speed:Number = 4;
		var leftPressed:Boolean = false;
		var rightPressed:Boolean = false;
		public function Engine():void{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		public function init(e:Event) {
			ourShip.x = stage.stageWidth / 2;
			ourShip.y = stage.stageHeight - 50;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeys);
			stage.addEventListener(KeyboardEvent.KEY_UP, removal);
			stage.addChild(ourShip);
			addEventListener(Event.ENTER_FRAME, EnteringFrame);
			removeEventListener(Event.ADDED_TO_STAGE, init);
		}
		public function checkKeys(e:KeyboardEvent):void {
			if((e.keyCode == 37) || (e.keyCode == 65)){
				leftPressed = true;
			}
			if((e.keyCode == 39) || (e.keyCode == 68)){
				rightPressed = true;
			}
		}
		public function EnteringFrame(e:Event) {
			if (leftPressed && ourShip.x > 20){
				ourShip.x -= speed;
			}
			if (rightPressed && ourShip.x < 530){
				ourShip.x += speed;
			}
		}
		public function removal(e:KeyboardEvent):void {
			if((e.keyCode == 37) || (e.keyCode == 65)){
				leftPressed = false;
			}
			if((e.keyCode == 39) || (e.keyCode == 68)){
				rightPressed = false;
			}
		}
	}
}

Some credit to ‘BraydenBlack’ and ‘iBringHam’ from GDR for tweaking this a little bit. 😛

Explain yourself

17 Jul

Why wasn’t I posting on this site/on GDR like I used to be? Well…

The thing is, that I found a great place where I am learning stuff like Wrestling and where I can work-out every week-day. Unfortunately, I had to cut back time on some things, which included learning AS3 as well as staying on GDR. Ahhhh well, this really isn’t meant to be a filler post anyhow. So I’ll just add an extra tip for you programmers who couldn’t care less…

in order to rotate a MovieClip/Sprite, you write the following within an onClipEvent(enterFrame) handler (for as2people):

myMovieClip._rotation += 1;

Depending on your FPS, this movieClip should be rotating slowly clockwise.

Arrays: Simple tools of the trade. (AS2 & AS3 style)

16 Jul
The following code is in ActionScript 2.
_root.myArr = [1,2,3,4,5];
_root.myArr.push(6);
//This should result in "1,2,3,4,5,6"
trace(myArr);
_root.myArr.splice(1,2,"X","Y");
//This should result in "1,X,Y,4,5,6"
trace(myArr);

Simply put, in ActionScript 2, an array can be made
as simply as just creating a name (like "myArr") and
making it equal a bracket of preset values.
However, unlike languages like Java
(arrays... not ArrayLists),
arrays in ActionScript2 and ActionScript3 can "pushed".
This means you can place values at the end of the array.
This is incredibly helpful for situations where you
need to group a set of values together.
Especially values that are created mid-game.
(Think particles which is a future topic)

The following code is for ActionScript 3.

package {
	import flash.display.Sprite;
	public class MainDoc extends Sprite {
		private var myArr:Array = new Array(1,2,3,4,5);
		public function MainDoc() {
			myArr.push(6);
			trace("After pushing '6': " + myArr);
			myArr.shift();
			trace("After Shift: " + myArr);
			//shift removes the first value
			myArr.unshift(1);
			trace("After Unshift of 1: " + myArr);
			//by UNshifting a value (this time it happened to be the original first value of '1')
			//you put it at the front of the array.
			trace("Popped: " + myArr.pop());
			//Pop is a method that removes the last value of the array ('6') and returns it.

			//You can find more methods/properties of arrays in the link below!
		}
	}
}

The link for more array fun. But I'm not quite sure
if all these methods/properties would work in AS2.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

Ternary whoozawhats?

6 Jul

Are you some sort of code-efficiency freak? Yep… I figured you were. If you don’t know about this already, then it will be a shock when I tell you that you could cut parts of your code in half its line size!!!

var bool:Boolean = false;
if(bool == false){
    trace("bool is definitely false");
} else {
    trace("bool is definitely true");
}

That’s the if/else simply-read way…
While it may be a little bit easier to comprehend, this saves a little more space if placed in the stead of if/else statements.

var bool:Boolean = false;
trace(bool ? "Bool is clearly true":"Bool is definitely false");

Breaking it down…
3 unnecessary lines cut. While ternary operators may be a little bit more difficult to read, it is quite simple once analyzed.
Let’s just look at how the typical ternary operator is.

The following is the basic template…

condition ? ifConditionIsTrue:ifConditionIsFalse

If it is your first time working with these… please take note of your condition, because Example A has a different output than Example B.

Example A

var iAmReading:Boolean = false;
trace(iAmReading ? "I'm reading right now... please keep quiet":"Sorry, too busy putting this online.");

Example B

var iAmReading:Boolean = true;
trace(iAmReading ? "I'm reading right now... please keep quiet":"Sorry, too busy putting this online.");