Animating Sprite Scale Properties

John Moran stumped me last night with a seemingly simply idea. To scale a circle on a button press. Basically, this is a 4 part process: 1) make the objects and add them to the stage 2) create two events: a click event and an enterframe event 3) target the to-be-scaled object 4) set some conditions for the animation to end, as we don’t want the scaled object to keep scaling and thus blow up visitors’ computers.

Part three was the stumper in the lab flurry, with lots of students needing aid. Well, I don’t like to leave things hanging so after a sandwich last night I did some testing and came up with the following:

//First let’s lay down some variables to be used
//a scale speed. It can be as fast as required.

var speed:Number = .4;

//the button (which is a button symbol in the library
//with linkage class Ball, titled ball)

var ball:Ball = new Ball();

//position the button
ball.x = ball.y = 20;

//add the ball (could use keyword this.addChild)
addChild(ball);

//I like an init() function to start, which adds click function to the button
function init() {
ball.addEventListener(MouseEvent.CLICK, addOrb);
}

//now write the addOrb function
function addOrb(event:MouseEvent):void {

//add the orb, a circle movieclip in the library with name orb, with
//linkage Class Orb
var orb:Sprite = new Orb();
addChild(orb);

//give some position
orb.x = 350;
orb.y = 300;

//declare some scale
orb.scaleX = 1;
orb.scaleY = 1;

//now the fun part: add enterframe keys and a new function growOrb
//which is the part we were missing yesterday. John, this is for
//classic “casting”
orb.addEventListener(Event.ENTER_FRAME, growOrb);
}

//okay, now’s the trick. We’re going to target the above orb by casting it
//with a new object to scale, which could be a sprite or a movieclip
//I’ve given growOrb a required argument orb:Event. Most coders will use
//growOrb(event:Event) or (evt:Event) or (e:Event), but I’ve chosen an arbitrary
//word “orb” just to illustrate how the code is targeting the orb sprite
function growOrb(orb:Event):void {

//create a sprite to scale and target orb
var grower:Sprite = Sprite(orb.target);

//supply a check variable
var roof:Number = 10;

//use += to increment the scale
grower.scaleX += speed;
grower.scaleY += speed;

//now apply a check, because we don’t want scale to persist. That
//would be a killer. It’s a simple if statement
if(grower.scaleX > roof) {

//when the condition is met remove the listener
//and the button if desired for clean up. Note that design wise
//this is a one time event
grower.removeEventListener(Event.ENTER_FRAME, growOrb);
removeChild(ball);
}
}

//don’t forget to call init() to get things going
init();

Here’s what it looks like. Click on the orange ball button:

IFs for Spring Are Online

Works of IF from the 2009 New Media Perspectives and Digital Narratives classes are available to play online.

Check them out here.

Flash Items on YouTube

A few You Tube video tutorials on instance names and frame labels by James Kyle and an additional bit on Actionscript 3 basics:

Instance Names
Frame Labels
OnEnterFlash on AS3 basics

AS3 Code for Frame Management

As asked for here’s the code for managing frame animations:

stop(); //stops the movie on the first frame
box1.addEventListener(MouseEvent.CLICK, goToPage1);
function goToPage1(event:MouseEvent):void {
gotoAndStop(”page1″);
}

In addition, you might try adding a circle movieclip to the stage in the first frame of the main timeline. Give the circle an instance name of circle.

Then supply some code in an actions layer on the first frame:

circle.x = 10;
circle.y = 20;

var speed:Number = 10;
var rightWall:Number = 400;

addEventListener(Event.ENTER_FRAME, goCircle);

function goCircle(event:Event):void {
circle.x += speed;
if(circle.x > rightWall) {
circle.x = rightWall;
speed = 0;
removeEventListener(Event.ENTER_FRAME, goCircle);
}
}

The above examples illustrate a mouse event and a frame event.

Sony Releases New Stupid Piece of S**t That Doesn’t F**king Work


Sony Releases New Stupid Piece Of S**t That Doesn’t F*****g Work

[NMP09] IF 02 – Actions & Rules

Here is the code I demo’ed on Monday morning about actions and the after, before, carry out, and instead rules: Continue reading [NMP09] IF 02 – Actions & Rules

[DN09] Scenes Part 02 – Cut Scenes

Here is the cut scene demonstration I did last Tuesday night: Continue reading [DN09] Scenes Part 02 – Cut Scenes

[DN09] Scenes and Regions Part 01

Here is the code for one of the examples I demonstrated last class: Continue reading [DN09] Scenes and Regions Part 01

[NMP09] Interactive Fiction 01

My I7 tutorial page is located here. You can find basic information plus more sophisticated tricks as well beyond the documentation included with the software.

Here is the code I was demo’ing in class: Continue reading [NMP09] Interactive Fiction 01

Etherpad

Real-time document collaboration at Etherpad.

But this is really cool.

Thanks to gTa for this.

The 11(?) Principles of E-Poetry

What it is and what it ain’t. Stephanie Strickland poses some ideas towards (and away from) an e-literature aesthetic(s). Or something like that.

Born Digital by Stephanie Strickland

Worth comparing with Manovich’s five principles of new media.

Everything You Ever Wanted to Know About Pac-Man

The Pac-Man Dossier

Tracking Wikipedia

For those you wondering about relationships and how to build them, The Palo Alto Research Center has recently build software for tracking changes to Wikipedia pages called WikiDashboard, an incredibly useful service for researchers.

This, for example, is the search result for hypertext, which we were lamenting a few weeks ago. The page has changed a lot, updated by experts in the field.

At the dashboard site, you can track items in which you may be interested.

Interactive Fiction Writing Month

if_writing_month

A Night of Comedy and Satire

Tunxis Community College Presents
A Night of Comedy, Satire, and Theater

“The Book of Leviticus Show,” “Kitty the Waitress,” “Funeral Parlor”* by Christopher Durang. Directed by George Sebastian-Coleman

“The Hail Mary Pass” by Patrice Hamilton. Directed by Patrice Hamilton

Here are the audition dates:

Wed. 5:30-8:30pm February 18
Rooms 6-127 & 6-128

Sat. 10am-12 pm February 21
Rooms 210 & 215

Call Backs
Sat. 12:30-3:00 pm 2/21/09
Rooms 210 & 215

Email tunxiscollege at gmail.com for Audition Info Sheet and remember to bring a copy of your picture to audition!

Pick up copies of the plays at the Tunxis Library

Dress Rehearsal: April 30, 2009

Performance Dates: Friday Night, May 1, 2009 and Saturday Night, May 2, 2009