Showing posts with label as3. Show all posts
Showing posts with label as3. Show all posts
Thursday, February 5, 2015
Sample code from todays class on AS3 Tweens 2011 02 17
I talked about the AS3 Tween class today and how it can be used to create motion tweens using AS3 code. For the example discussed in class, I used a for loop to create multiple sprite instances, and multiple tween objects to animate those sprites. The size, color and movement properties of each moving sprite were randomized as well. I also talked about the MOTION_FINISH event related to the AS3 Tween class, and how it can be used with the yoyo() method to make the tweens loop back and forth. Some of my students asked if I could post the sample code from our session today about the AS3 Tween class, so Im posting it here. I also created another version that has more randomized properties. The AS3 Tween class is great for creating simple motion tween animations using code.
Read more »
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.Sprite;
var nSprites:Number = 25;
var aSprites:Array = new Array();
var aTweenX:Array = new Array();
var aTweenY:Array = new Array();
for (var i:Number = 0; i < nSprites; i++)
{
var nRadius:Number = Math.random() * 25 + 10;
var nColor:Number = Math.random() * 0xFFFFFF;
var nStartX:Number = Math.random() * stage.stageWidth;
var nEndX:Number = Math.random() * stage.stageWidth;
var nStartY:Number = Math.random() * stage.stageHeight;
var nEndY:Number = Math.random() * stage.stageHeight;
aSprites[i] = new Sprite();
aSprites[i].graphics.beginFill(nColor);
aSprites[i].graphics.drawCircle(0,0,nRadius);
aSprites[i].graphics.endFill();
addChild(aSprites[i]);
aTweenX[i] = new Tween(aSprites[i], "x", Elastic.easeInOut, nStartX, nEndX, 3, true);
aTweenY[i] = new Tween(aSprites[i], "y", Elastic.easeInOut, nStartY, nEndY, 7, true);
aTweenX[i].addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
aTweenY[i].addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
}
function onMotionFinish(e:TweenEvent):void
{
e.target.yoyo();
}Heres another version with even more randomization. The duration for each tween is randomized. The easing used is also randomized. The different easing functions are placed in an Array. Then we get a random number thats anywhere from 0 to the highest index value in the array, and use that to retrieve an easing type randomly from the easing functions Array: import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.display.Sprite;
var nSprites:Number = 25;
var aSprites:Array = new Array();
var aTweenX:Array = new Array();
var aTweenY:Array = new Array();
var aEasing:Array = new Array(None.easeNone, Back.easeIn, Back.easeOut, Back.easeInOut, Bounce.easeIn, Bounce.easeOut, Bounce.easeInOut, Elastic.easeIn, Elastic.easeOut, Elastic.easeInOut, Regular.easeIn, Regular.easeOut, Regular.easeInOut, Strong.easeIn, Strong.easeOut, Strong.easeInOut);
for (var i:Number = 0; i < nSprites; i++)
{
var nRadius:Number = Math.random() * 25 + 10;
var nColor:Number = Math.random() * 0xFFFFFF;
var nStartX:Number = Math.random() * stage.stageWidth;
var nEndX:Number = Math.random() * stage.stageWidth;
var nStartY:Number = Math.random() * stage.stageHeight;
var nEndY:Number = Math.random() * stage.stageHeight;
var nDurationX:Number = Math.random() * 5 + 2;
var nDurationY:Number = Math.random() * 5 + 2;
var nEasingX:Number = Math.floor(Math.random() * aEasing.length);
var nEasingY:Number = Math.floor(Math.random() * aEasing.length);
aSprites[i] = new Sprite();
aSprites[i].graphics.beginFill(nColor);
aSprites[i].graphics.drawCircle(0,0,nRadius);
aSprites[i].graphics.endFill();
addChild(aSprites[i]);
aTweenX[i] = new Tween(aSprites[i], "x", aEasing[nEasingX], nStartX, nEndX, nDurationX, true);
aTweenY[i] = new Tween(aSprites[i], "y", aEasing[nEasingY], nStartY, nEndY, nDurationY, true);
aTweenX[i].addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
aTweenY[i].addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
}
function onMotionFinish(e:TweenEvent):void
{
e.target.yoyo();
}Tuesday, January 27, 2015
Creating a Pentomino game using AS3 Part 2
Today well add automatic sizing and positioning to our grid.
I plan for my game to have multiple levels (maps), and each map may differ in size and shape. Since some maps can be rather big and take a lot of space, when smaller maps take much less space, there is a problem with positioning the grid properly so that most of the available screen is used.
I am going to reserve some space for a toolbar panel, which will later contain the available shapes and other game info. My game dimensions right now are 720x460. The panel will reserve 200 pixels in width, including the space left to the right screen edge. That means the width of the panel will be 195 pixels, its height - 450 pixels, because Im leaving 5 pixels for the top, left and bottom margins. Keeping that in mind, the X and Y coordinates of the panel would be 520, 5 pixels.
This leaves us 520x460 free space. Inside of this rectangle, well put our grid. However, once again, remember that Im leaving 5 pixel margins for top, bottom, left and right, so the actual rectangle size is 510x450. Thats the size that we want to fit our grid in, making it take up as much space as possible.
Go to constructor of pentomino_game() class and call a calculateGrid() function before adding gridShape to stage:
First thing we do in the function is calculate how many columns and rows there are:
Write down a memo to remember the numbers were working with:
Now, using the columns value we can calculate how wide a cell should be to be able to fit in the 510 width rectangle.
Then we calculate the real width and height of the grid in pixels:
Now we can set gridStartX value, which is the left margin of the grid. Id set it to 5, but its important to remember that gridCellWidth is a rounded value, it might have been a fraction and if we just set gridStartX to 5, the right marin might be bigger or smaller than 5. Thats why we simply substract width from 520 and divide it by 2, to have the same margin from left and right. This might not always be 5 (it will be around 5 pixels then), but it will look nice since the margins are the same.
Now, we check if height fits in the 450 high rectangle. If so, then we set gridStartY using the same method we set gridStartX:
If height is greater than or equals 450, we need to recalculate the whole thing, but set the height first instead of the width. Then we calculate height and width values again, and set gridStartX and gridStartY just like before:
Full function:
The results can be observed when you use differently sized maps:


Full code:
Thanks for reading!
Read more »
I plan for my game to have multiple levels (maps), and each map may differ in size and shape. Since some maps can be rather big and take a lot of space, when smaller maps take much less space, there is a problem with positioning the grid properly so that most of the available screen is used.
I am going to reserve some space for a toolbar panel, which will later contain the available shapes and other game info. My game dimensions right now are 720x460. The panel will reserve 200 pixels in width, including the space left to the right screen edge. That means the width of the panel will be 195 pixels, its height - 450 pixels, because Im leaving 5 pixels for the top, left and bottom margins. Keeping that in mind, the X and Y coordinates of the panel would be 520, 5 pixels.
This leaves us 520x460 free space. Inside of this rectangle, well put our grid. However, once again, remember that Im leaving 5 pixel margins for top, bottom, left and right, so the actual rectangle size is 510x450. Thats the size that we want to fit our grid in, making it take up as much space as possible.
Go to constructor of pentomino_game() class and call a calculateGrid() function before adding gridShape to stage:
public function pentomino_game()
{
// default map
mapGrid = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
// grid settings
calculateGrid();
addChild(gridShape);
gridShape.x = gridStartX;
gridShape.y = gridStartY;
// draw tiles
drawGrid();
}
First thing we do in the function is calculate how many columns and rows there are:
var columns:int = mapGrid[0].length;
var rows:int = mapGrid.length;
Write down a memo to remember the numbers were working with:
// free size: 520x460
// fit in: 510x450
Now, using the columns value we can calculate how wide a cell should be to be able to fit in the 510 width rectangle.
// calculate width of a cell:
gridCellWidth = Math.round(510 / columns);
Then we calculate the real width and height of the grid in pixels:
var width:int = columns * gridCellWidth;
var height:int = rows * gridCellWidth;
Now we can set gridStartX value, which is the left margin of the grid. Id set it to 5, but its important to remember that gridCellWidth is a rounded value, it might have been a fraction and if we just set gridStartX to 5, the right marin might be bigger or smaller than 5. Thats why we simply substract width from 520 and divide it by 2, to have the same margin from left and right. This might not always be 5 (it will be around 5 pixels then), but it will look nice since the margins are the same.
// calculate side margin
gridStartX = (520 - width) / 2;
Now, we check if height fits in the 450 high rectangle. If so, then we set gridStartY using the same method we set gridStartX:
if (height < 450) {
gridStartY = (450 - height) / 2;
}
If height is greater than or equals 450, we need to recalculate the whole thing, but set the height first instead of the width. Then we calculate height and width values again, and set gridStartX and gridStartY just like before:
if (height >= 450) {
gridCellWidth = Math.round(450 / rows);
height = rows * gridCellWidth;
width = columns * gridCellWidth;
gridStartY = (460 - height) / 2;
gridStartX = (520 - width) / 2;
}
Full function:
private function calculateGrid():void {
var columns:int = mapGrid[0].length;
var rows:int = mapGrid.length;
// free size: 520x460
// fit in: 510x450
// calculate width of a cell:
gridCellWidth = Math.round(510 / columns);
var width:int = columns * gridCellWidth;
var height:int = rows * gridCellWidth;
// calculate side margin
gridStartX = (520 - width) / 2;
if (height < 450) {
gridStartY = (450 - height) / 2;
}
if (height >= 450) {
gridCellWidth = Math.round(450 / rows);
height = rows * gridCellWidth;
width = columns * gridCellWidth;
gridStartY = (460 - height) / 2;
gridStartX = (520 - width) / 2;
}
}
The results can be observed when you use differently sized maps:


Full code:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
/**
* Open-source pentomino game engine
* @author Kirill Poletaev
*/
public class pentomino_game extends MovieClip
{
private var mapGrid:Array = [];
private var gridShape:Sprite = new Sprite();
private var gridStartX:int;
private var gridStartY:int;
private var gridCellWidth:int;
public function pentomino_game()
{
// default map
mapGrid = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
// grid settings
calculateGrid();
addChild(gridShape);
gridShape.x = gridStartX;
gridShape.y = gridStartY;
// draw tiles
drawGrid();
}
private function calculateGrid():void {
var columns:int = mapGrid[0].length;
var rows:int = mapGrid.length;
// free size: 520x460
// fit in: 510x450
// calculate width of a cell:
gridCellWidth = Math.round(510 / columns);
var width:int = columns * gridCellWidth;
var height:int = rows * gridCellWidth;
// calculate side margin
gridStartX = (520 - width) / 2;
if (height < 450) {
gridStartY = (450 - height) / 2;
}
if (height >= 450) {
gridCellWidth = Math.round(450 / rows);
height = rows * gridCellWidth;
width = columns * gridCellWidth;
gridStartY = (460 - height) / 2;
gridStartX = (520 - width) / 2;
}
}
private function drawGrid():void {
gridShape.graphics.clear();
var width:int = mapGrid[0].length;
var height:int = mapGrid.length;
var i:int;
var u:int;
// draw background
for (i = 0; i < height; i++) {
for (u = 0; u < width; u++) {
if (mapGrid[i][u] == 1) drawCell(u, i, 0xffffff, 1, 0x999999);
}
}
}
private function drawCell(width:int, height:int, fill:uint, thick:Number, line:uint):void {
gridShape.graphics.beginFill(fill);
gridShape.graphics.lineStyle(thick, line);
gridShape.graphics.drawRect(width * gridCellWidth, height * gridCellWidth, gridCellWidth, gridCellWidth);
}
}
}
Thanks for reading!
Monday, January 19, 2015
AS3 Dragging and Dropping Tutorial AS3 startDrag and AS3 stopDrag
Exercise File:
as3-drag-and-drop.fla - Flash CS4/CS5
as3-drag-and-drop.fla - Flash CS3
In this tutorial, were going to learn how to create simple AS3 drag and drop functionality using the AS3 startDrag() and AS3 stopDrag() methods. The exercise file can be downloaded from the link above, and the AS3 dragging and dropping code is provided below along with comments that explain the code.
Here is the ActionScript 3 code:
Heres the same code for ActionScript 3 dragging and dropping, but with comments:
Read more »
as3-drag-and-drop.fla - Flash CS4/CS5
as3-drag-and-drop.fla - Flash CS3
In this tutorial, were going to learn how to create simple AS3 drag and drop functionality using the AS3 startDrag() and AS3 stopDrag() methods. The exercise file can be downloaded from the link above, and the AS3 dragging and dropping code is provided below along with comments that explain the code.
[VIEW DEMO]
Here is the ActionScript 3 code:
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
square_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag(false, new Rectangle(75,50,350,250));
}
function drop(e:MouseEvent):void
{
stopDrag();
}Heres the same code for ActionScript 3 dragging and dropping, but with comments:
/*Use the AS3 startDrag() method of the MovieClip class in
order to let the user drag MovieClip instances all over the stage.
Example:
myMovieClip.startDrag();
You can call this method whenever the user presses on
a MovieClip instance, so that the user can start
dragging it once he or she presses on the MovieClip.
The event for that would be MouseEvent.MOUSE_DOWN.
To stop the user from being able to drag the MovieClip
instance, you use the AS3 stopDrag() method. You can call
this whenever the user releases the mouse button so that
the object being dragged will be released the same time
the mouse button is released. The event for that would
be MouseEvent.MOUSE_UP.
To summarize:
Call the AS3 startDrag() method whenever MOUSE_DOWN happens, and call
the AS3 stopDrag() method whenever MOUSE_UP happens. In other words,
enable dragging while the user is pressing on the movie
clip, and disable dragging when the user releases it. */
/*Add the MouseEvent.MOUSE_DOWN listeners to the MovieClip
instances that youd like the user to be able to drag.*/
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
square_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
/*Add the MouseEvent.MOUSE_UP listener to the stage.*/
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
/*This is the event listener function for the MOUSE_DOWN
event, which enables dragging.*/
function drag(e:MouseEvent):void
{
e.target.startDrag(false, new Rectangle(75,50,350,250));
/*This will enable dragging for whichever object is
currently being pressed.
It accepts 2 parameters (these are optional):
1. lockCenter:Boolean - true or false
If set to true, the objects registration point locks
to the mouse pointer. If false, whatever part of the
object that was clicked on locks to the pointer.
2. Bounds:Rectangle
Lets you create a rectangular boundary that restricts where
the object can be dragged. You create a rectangle
object that will serve as the boundary:
new Rectangle (x,y,width,height)
This rectangle will not be seen. The rectangle shape
that you see on the stage of this exercise file is
just a drawing to help illustrate where the
boundary is. Also, its not the shape itself that
stays inside the boundary, but the MovieClip instances
registration point. So parts of the MovieClip instance
can still move beyond the boundary.*/
}
/*This is the event listener function for the MOUSE_UP
event, which disables dragging.*/
function drop(e:MouseEvent):void
{
stopDrag();
/*Simply call the stopDrag() method, and whichever
object is currently being dragged will then be
dropped.*/
}
/*Why add the MOUSE_UP event listener to the stage instead
of to the MovieClip instances?
As long as the mouse pointer is within the stage, then
we can be sure that the MOUSE_UP event will be detected.
The problem with adding the MOUSE_UP event listener
to any of the MovieClip instances is that sometimes,
the MovieClip instance gets left behind when the user
drags too fast. If the user releases the mouse button
and the mouse pointer is NOT on the actual MovieClip
instance, then the MOUSE_UP event wont get dispatched.
This will cause the MovieClip instance to get stuck
to the mouse pointer even though the mouse button has
already been released. So it would be better to add the
event listener to the stage instead.*/And that concludes this basic example on AS3 dragging and dropping using the AS3 startDrag() and the AS3 stopDrag() methods.
Subscribe to:
Posts (Atom)