Actionscript 3 Classes, Drag Class.

This was my first attempt at actionscript 3.0, i had heard it was more of a programming language then the previous 2.

So Alex Fish, a good friend of mine and me, began to look at actionscript 3.0, my background was in flash with as2, while Alex had previous programming experience with processing, css, html, and various other web languages. So we began to delve into the unknown…

By lunch time we had learnt some of the key basics of actionscript 3.0, one of the main think I learnt was about classes I was use to programming everything on the first frame of my file, now i just simply made a document class and compiled and voila, nothing in the .fla. So we began to work together, we decided we wanted to make a drag and drop class because this would be a useful class that would be helpful for me in the future, and also be something Alex could use with his latest project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// AS3 drag and drop class
// Alex Fish and Josh Shard
 
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
 
public class drag extends Sprite {
private var eventSprite:Sprite;
private var dragging=0;
private var speed=1;
private var xMouse:Number;
private var yMouse:Number;
public function drag() {
init();
}
private function init():void {
//Stops the movie changing scale
stage.scaleMode=StageScaleMode.NO_SCALE;
//
//Places in the top left of window
stage.align=StageAlign.TOP_LEFT;
 
//
//Place Sprite on the stage
eventSprite=new Sprite;
addChild(eventSprite);
 
//
// Draws a circle and fills it with the hexidecimal colour.
eventSprite.graphics.beginFill(0xffffff);
eventSprite.graphics.drawCircle(0,0,50);
eventSprite.graphics.endFill();
//
//Places the circle(sprite) in the centre of the stage
eventSprite.x=stage.stageWidth / 2;
eventSprite.y=stage.stageHeight / 2;
 
//
//Adds code to listen for mouse down and mouse up
eventSprite.addEventListener(MouseEvent.MOUSE_DOWN,onMousePressedDown);
eventSprite.addEventListener(MouseEvent.MOUSE_UP,onMouseReleased);
eventSprite.addEventListener(Event.ENTER_FRAME,onLoop);
 
}
//
// This is our on enterframe function to start the drag. And stop it.
private function onLoop(event:Event):void {
//trace("entering frame");
if (dragging == 1) {
eventSprite.x=mouseX;
eventSprite.y=mouseY;
}
eventSprite.x += speed;
}
//
// This function handles the event when the mouse is pressed
// on our sprite.
private function onMousePressedDown(event:MouseEvent):void {
//trace("Mouse Down");
dragging=1;
speed += 1;
}
//
// This function handles the event when the mouse is released
// on our sprite.
private function onMouseReleased(event:MouseEvent):void {
//trace("Mouse Up");
dragging=0;
}
}
}

This is the code we put together, and worked pretty well, I added a few additions to mine, including a speed factor meaning everytime you press down the ball gets faster. It also moves towards the edge of the screen which was a bit of simple animation I was exploring. You can check the compiled version here.  Over the next weeks I will be exploring more actionscript while also adding some recent as2 tutorials to my blog.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*