Wednesday, October 11, 2006

SWFObject

This Javascript Flash Player detection and embed script works great. No annoying dotted line around the Flash.

http://blog.deconcept.com/swfobject/

Tuesday, August 22, 2006

ClickTAG and Flash Banner Specs

This is a great article on how to do Flash banners.

Tuesday, May 23, 2006

Depth Sorting

Use this code to sort objects in 3d space.



function sortDepthOfField(clipArray) {

clipArray.sort(clipYSort);

for (var i = 0; i<clipArray.length; i++) {
clipArray[i].swapDepths(i);
}
}
sortOnY = function(a,b){
if(a._y > b._y){
return 1;
}else if(a._y < b._y){
return -1;
}else{
return 0;
}
}
theClips = [bird1, bird2, bird3, bird4, bird5, bird6];
this.onEnterFrame = function() {
sortDepthOfField(theClips);
};

Tuesday, May 09, 2006

Shared Objects (Flash Cookies)

Here is the code to set up the shared object:
//HIGH SCORE SHARED OBJECT =======================================
// create a new shared object named "zionshighscore"
my_so = SharedObject.getLocal("zionshighscore");
// set a new variable (highScore) to 0
if (my_so.data.highScore == undefined) {
my_so.data.highScore = 0;
}
// this saves the shared object immediately as opposed to when the swf shuts down
my_so.flush;
//my_so.data.highScore = 0;
highScore_mc.highScore.text = "$"+my_so.data.highScore+".00";
// show or hide high score display
if (my_so.data.highScore == 0) {
highScore_mc._alpha = 0;
} else {
highScore_mc._alpha = 100;
}
zeroHighScore.onRelease = function() {
my_so.data.highScore = 0;
highScore_mc.highScore.text = "$"+my_so.data.highScore+".00";
};
//

This code sets the shared object to a new value:
updateHighScore = function () {
if (theScore>my_so.data.highScore) {
my_so.data.highScore = theScore;
showPrompt("New High Score!", 4000);
}
};

Popup Windows from Flash

researchTools.toolsText.link1a.onRelease = function() {
getURL("javascript:NewWindow=window.open('http://money.cnn.com/pf/features/lists/nar_3q05/','newWin','width=620,height=550,left=100,top=100, toolbar=Yes,location=No,scrollbars=Yes,status=Yes,resizable=Yes,fullscreen=No'); NewWindow.focus(); void(0);");
};

Timer

Look in the Equity Adventure code in gameplay.as

Shuffle Array

shuffleArray = function (theArray) {
// (Shuffle 10 times just for the heck of it)
for (i=0; i<10; i++) {
theArray.sort(function (a, b) {
return random(3)-1;
});
}
};




Array.prototype.shuffle = function() {
var Arr2 = new Array();
while (this.length>0) {
Arr2.push(this.splice(Math.round((this.length-1)*Math.random()), 1));
}
return Arr2;
};
theSlides=[1,2,3,4]
theSlides.shuffle();

Basic Class Structure


class Dog extends MovieClip {
var speedLimit:Number = 5;
var point:Object;
var dogState:String = "stay";
var trash:MovieClip;
function Dog() {
//constructor
}
function onEnterFrame():Void {
//enterFrame
}
private function adjustVelocity(a, b):Void {
//private function
}
}
function onRelease():Void {
//onrelease
}
function onDragOut():Void {
//ondragout
}
function onRollOver():Void {
//onrollover
}
function onRollOut():Void {
//onrollout
}

Sound Objects

For sounds to not conflict with each other, they need to be assigned to movieclips - I think. This also works:

function newLevelMusic() {
var newLevel:Sound = new Sound();
newLevel.attachSound("newLevel");
newLevel.start();
}
newLevelMusic()

Here is a function that kind of works as a sound machine:

//sfxSpeaker
playSound = function (theID, theVolume, repeatTimes) {
voiceCounter++;
sfxSpeaker.createEmptyMovieClip("newVoiceMC"+voiceCounter, voiceCounter+10);
sfxSpeaker["newVoiceMC"+voiceCounter].theSound = new Sound(sfxSpeaker["newVoiceMC"+voiceCounter]);
sfxSpeaker["newVoiceMC"+voiceCounter].theSound.attachSound(theID);
sfxSpeaker["newVoiceMC"+voiceCounter].theSound.setVolume(theVolume);
sfxSpeaker["newVoiceMC"+voiceCounter].theSound.start(0, repeatTimes);
};

Tweening Using Flash's Built In Tweens

First you need to pull in tween classes:

import mx.transitions.Tween;
import mx.transitions.easing.*;
this.lvlTween = new Tween(targetMc, "_y", Bounce.easeOut, -400, 200, 30);
tweenInstance.onMotionFinished = function() {
// ...
};

For more info, search for "Using the Tween class" in Flash help

Data.as Arrays

Here is an easy to understand array file that to me is easier than XML. This of course requires recompiling of the swf but for data that doesnt change this is nice.


//database
//leaves, timerCycles, dogsArray
var gameLeaves:Array = [];
var gameTicks:Array = [];
var gameLeafFallSpeeds:Array = [];
//level 0
gameLeaves[0] = 50;
gameTicks[0] = 600;
gameLeafFallSpeeds[0] = 15;
//level 1
gameLeaves[1] = 100;
gameTicks[1] = 800;
gameLeafFallSpeeds[1] = 15;
//level 2
gameLeaves[2] = 200;
gameTicks[2] = 1100;
gameLeafFallSpeeds[2] = 10;
//level 3
gameLeaves[3] = 300;
gameTicks[3] = 1200;
gameLeafFallSpeeds[3] = 10;
//level 4
gameLeaves[4] = 400;
gameTicks[4] = 1700;
gameLeafFallSpeeds[4] = 5;
//level 5
gameLeaves[5] = 600;
gameTicks[5] = 2100;
gameLeafFallSpeeds[5] = 5;
//level 6
gameLeaves[6] = 800;
gameTicks[6] = 2500;
gameLeafFallSpeeds[6] = 5;

#include

#include "data.as"
#include "main.as"

Display Quality

_quality = "LOW";
_quality = "MEDIUM";
_quality = "HIGH";
_quality = "BEST";

Preloading

To prevent all of the classes from loading in frame 1 (which causes an annoying blank screen for a long time before the preloader appears):
-I set them to load in a later frame (8).
-Movieclips in the library that have Classes attached to them need to have the "export in first frame" checkbox unchecked for this to work.
-For the classes to load there needs to be at least one instance on the stage somewhere in a timeline or the Class won't
load. For example, in the leaf game there are leaves that are pulled dynamically out of the library that have the "Leaf" class attached. These leaves weren't working until I realized that there was no instance of a leaf anywhere in the movie. I created an instance in "_root.leafHost.leafInit" and the leaves started working becuase they had been instantiated
by this new clip.
To preload sounds the same principles apply. There needs to be a frame with a sound attached
for attached sounds to work.
Here is some preloader code:

this.onEnterFrame = function() {
if (_framesloaded == _totalframes) {
delete this.onEnterFrame;
gotoAndStop("intro");
} else {
theProgress = "Loading "+(Math.floor(this.getBytesLoaded()/1000))+"K of "+(Math.floor(this.getBytesTotal()/1000))+"K";
preloadBar._width = (241*this.getBytesLoaded())/this.getBytesTotal();
}
};

Here is another preloader:

this.onEnterFrame = function() {
//trace("...");
preloader_mc.loaderText.text = "LOADING "+Math.floor(this.getBytesLoaded()*.001)+"K OF "+Math.floor(this.getBytesTotal()*.001)+"K";
preloader_mc.loaderBar._width=(preloader_mc.loaderBG._width*this.getBytesLoaded())/this.getBytesTotal()
if (this.getBytesLoaded() == this.getBytesTotal()) {
delete this.onEnterFrame;
gotoAndStop("titlescreen");
}
};