Creating a Spinning Wheel in as3 that displays text when stopping - actionscript-3

I am trying to create a spinning wheel that will display a coupon code when it stops on a particular color. Right now it displays color at the bottom of the page but I would like to add in specific coupon codes associated with each wheel snippet that appear when the wheel stops Thanks in advance!
Here is the current code:
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
public final class Main extends Sprite
{
private var speed:Number = 0;
private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
private var line:Shape;
private var lastPaddle:String;
public final function Main():void
{
paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6);
listeners('add');
}
private final function listeners(action:String):void
{
if(action == 'add')
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
else
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
}
private final function startDraw(e:MouseEvent):void
{
line = new Shape();
addChild(line);
line.graphics.moveTo(mouseX, mouseY);
line.graphics.lineStyle(5, 0x00000, 0.3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
}
private final function drawLine(e:MouseEvent):void
{
line.graphics.lineTo(mouseX, mouseY);
}
private final function spinWheel(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
listeners('rm');
speed = line.height * 0.1;
removeChild(line);
line = null;
stage.addEventListener(Event.ENTER_FRAME, spin);
}
private final function spin(e:Event):void
{
/* Rotate Wheel */
wheel.rotationZ += speed;
/* Detect Value */
for(var i:int = 0; i < 6; i++)
{
if(indicator.hArea.hitTestObject(paddles[i]))
{
lastPaddle = paddles[i].name;
}
}
speed -= 0.1;
if(speed <= 0)
{
stage.removeEventListener(Event.ENTER_FRAME, spin);
speed = 0;
run(lastPaddle);
listeners('null');
}
}
function run(action:String):void
{
switch(action)
{
case 'p1':
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x644D9B, tintAmount:1}});
break;
case 'p2':
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xFFCC00, tintAmount:1}});
break;
case 'p3':
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x4789C2, tintAmount:1}});
break;
case 'p4':
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF15D5D, tintAmount:1}});
break;
case 'p5':
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x90CC6C, tintAmount:1}});
break;
case 'p6':
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF29C69, tintAmount:1}});
break;
}
}
}
}
EDIT:: This is the updated code per suggestions I added the wheel.p1.addChild(myText); at the end in the function run(action:String):void area. Is this the correct way of doing it or is there a better way. I also was unable to have the text show up in the live version.
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
import flash.text.TextFormat;
import flash.text.TextField;
public final class Main extends Sprite;
{
private var speed:Number = 0;
private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
private var line:Shape;
private var lastPaddle:String;
public final function Main():void
{
paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6);
listeners('add');
}
private final function listeners(action:String):void
{
if (action == 'add')
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
else
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
}
private final function startDraw(e:MouseEvent):void
{
line = new Shape();
addChild(line);
line.graphics.moveTo(mouseX, mouseY);
line.graphics.lineStyle(5, 0x00000, 0.3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
}
private final function drawLine(e:MouseEvent):void
{
line.graphics.lineTo(mouseX, mouseY);
}
private final function spinWheel(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
listeners('rm');
speed = line.height * 0.1;
removeChild(line);
line = null;
stage.addEventListener(Event.ENTER_FRAME, spin);
}
private final function spin(e:Event):void
{
/* Rotate Wheel */
wheel.rotationZ += speed;
/* Detect Value */
for (var i:int = 0; i < 6; i++)
{
if (indicator.hArea.hitTestObject(paddles[i]))
{
lastPaddle = paddles[i].name;
}
}
speed -= 0.1;
if (speed <= 0)
{
stage.removeEventListener(Event.ENTER_FRAME, spin);
speed = 0;
run(lastPaddle);
listeners('null');
}
}
function run(action:String):void
{
switch (action)
{
case 'p1' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x644D9B, tintAmount:1}});
var myText:TextField = new TextField();
myText.text = "10per";
wheel.p1.addChild(myText);
myText.textColor = 0xFF0000;
myText.x = 350;
myText.y = 485;
break;
case 'p2' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xFFCC00, tintAmount:1}});
break;
case 'p3' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x4789C2, tintAmount:1}});
break;
case 'p4' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF15D5D, tintAmount:1}});
break;
case 'p5' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x90CC6C, tintAmount:1}});
break;
case 'p6' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF29C69, tintAmount:1}});
break;
}
}
}
}

Here is the final code that worked how I needed it to! Thanks to those who helped.
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
import flash.text.TextFormat;
import flash.text.TextField;
public final class Main extends Sprite
{
private var speed:Number = 0;
private var paddles:Vector.<Sprite> = new Vector.<Sprite>();
private var line:Shape;
private var lastPaddle:String;
public final function Main():void
{
paddles.push(wheel.p1, wheel.p2, wheel.p3, wheel.p4, wheel.p5, wheel.p6);
listeners('add');
}
private final function listeners(action:String):void
{
if (action == 'add')
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.addEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
else
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDraw);
stage.removeEventListener(MouseEvent.MOUSE_UP, spinWheel);
}
}
private final function startDraw(e:MouseEvent):void
{
line = new Shape();
addChild(line);
line.graphics.moveTo(mouseX, mouseY);
line.graphics.lineStyle(5, 0x00000, 0.3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, drawLine);
}
private final function drawLine(e:MouseEvent):void
{
line.graphics.lineTo(mouseX, mouseY);
}
private final function spinWheel(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drawLine);
listeners('rm');
speed = line.height * 0.1;
removeChild(line);
line = null;
stage.addEventListener(Event.ENTER_FRAME, spin);
}
private final function spin(e:Event):void
{
/* Rotate Wheel */
wheel.rotationZ += speed;
/* Detect Value */
for (var i:int = 0; i < 6; i++)
{
if (indicator.hArea.hitTestObject(paddles[i]))
{
lastPaddle = paddles[i].name;
}
}
speed -= 0.1;
if (speed <= 0)
{
stage.removeEventListener(Event.ENTER_FRAME, spin);
speed = 0;
run(lastPaddle);
listeners('null');
}
}
function run(action:String):void
{
switch (action)
{
case 'p1' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x644D9B, tintAmount:1}});
var myFormat:TextFormat = new TextFormat();
myFormat.size = 30;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "10% off your order with coupon code:perc10";
addChild(myText);
myText.textColor = 0xFFFFFF;
myText.width = 624;
myText.height = 41;
myText.x = 43;
myText.y = 457;
break;
case 'p2' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xFFCC00, tintAmount:1}});
var myFormat:TextFormat = new TextFormat();
myFormat.size = 27;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "$5 off when you spend $20+ with coupon code:5iveoff";
addChild(myText);
myText.textColor = 0x6D6D6D;
myText.width = 676;
myText.height = 38;
myText.x = 22;
myText.y = 459;
break;
case 'p3' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x4789C2, tintAmount:1}});
var myFormat:TextFormat = new TextFormat();
myFormat.size = 30;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "20% off your order with coupon code:2ZERO";
addChild(myText);
myText.textColor = 0xFFFFFF;
myText.width = 618;
myText.height = 41;
myText.x = 43;
myText.y = 458;
break;
case 'p4' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF15D5D, tintAmount:1}});
var myFormat:TextFormat = new TextFormat();
myFormat.size = 27;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "$10 off when you spend $30+ with coupon code:10doll";
addChild(myText);
myText.textColor = 0xFFFFFF;
myText.width = 672;
myText.height = 38;
myText.x = 20;
myText.y = 459;
break;
case 'p5' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0x90CC6C, tintAmount:1}});
var myFormat:TextFormat = new TextFormat();
myFormat.size = 30;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "15% off your order with coupon code:fifperc";
addChild(myText);
myText.textColor = 0xFFFFFF;
myText.width = 618;
myText.height = 41;
myText.x = 50;
myText.y = 458;
break;
case 'p6' :
TweenMax.to(colorMC, 0.5, {colorTransform:{tint:0xF29C69, tintAmount:1}});
var myFormat:TextFormat = new TextFormat();
myFormat.size = 27;
var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.text = "$15 off when you spend $40+ with coupon code:fifdolla";
addChild(myText);
myText.textColor = 0xFFFFFF;
myText.width = 672;
myText.height = 38;
myText.x = 15;
myText.y = 459;;
break;
}
}
}
}

Related

how to use .visible properties in as3?

I just want to make some objects visible="false". But I can't. I can make some objects invisible, however it is not working for the objects in the array. Here is my codes. When score=3, objects should be invisible. It is a catching game. I use Adobe Flash CC.
Can u help me?
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.utils.getDefinitionByName;
public class CatchingGame extends MovieClip {
var catcher:Catcher;
var nextObject:Timer;
var objects:Array = new Array();
var score:int = 0;
const speed:Number = 7.0;
public function CatchingGame() {
catcher = new Catcher();
catcher.y = 350;
addChild(catcher);
setNextObject();
addEventListener(Event.ENTER_FRAME, moveObjects);
}
public function setNextObject() {
nextObject = new Timer(1000+Math.random()*1000,1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
nextObject.start();
}
public function newObject(e:Event) {
var goodObjects:Array = ["Circle1","Circle1","Circle1"];
var badObjects:Array = ["Square1","Square2", "Circle2"];
if (Math.random() < .5) {
var r:int = Math.floor(Math.random()*goodObjects.length);
var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
var newObject:MovieClip = new classRef();
newObject.typestr = "good";
} else {
r = Math.floor(Math.random()*badObjects.length);
classRef = getDefinitionByName(badObjects[r]) as Class;
newObject = new classRef();
newObject.typestr = "bad";
}
newObject.x = Math.random()*500;
addChild(newObject);
objects.push(newObject);
setNextObject();
}
public function moveObjects(e:Event) {
for(var i:int=objects.length-1;i>=0;i--) {
objects[i].y += speed;
if (objects[i].y > 400) {
removeChild(objects[i]);
objects.splice(i,1);
}
if (objects[i].hitTestObject(catcher)) {
if (objects[i].typestr == "good") {
score += 1;
}
if (score < 0) score = 0;
scoreDisplay.text = "Score:" +score;
removeChild(objects[i]);
objects.splice(i,1);
}
}
for(var k:int=objects.length-1;k>=0;k--){
if (score == 3){finished.text="Well Done!";
catcher.visible=false;
scoreDisplay.visible=false;
objects[k].visible=false; }
}
catcher.x = mouseX;
}
}
}
Bildiğim kadarıyla şu sekilde olması lazım
this[diziismi(i)].visible = false;
bu şekilde daha önceki projelerimin birinde kullandığımı ve çalıştığını hatırlıyorum

How do I remove eventlisteners and objects from an array upon game completion?

I have written a drag and drop game and, for the most part, it works.
It runs and does what I need it to do.
However, I cannot figure out two things.
How to remove the toy objects from the screen and re-add them after game over.
How to remove the event listeners for dragging/collision action after game over has initiated.
At the moment, after score is displayed you can still drop items in the toybox and make the score rise even when game over has been displayed.
Does anyone have any idea what I am doing wrong?
I would love to figure this out but it is driving me crazy.
Any help would be great.
Here is my code ...
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.Font;
import flash.filters.GlowFilter;
public class MainGame extends MovieClip {
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var tryAgain:TryAgain = new TryAgain;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000, 30);
var myText:TextField = new TextField;
var myText2:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var myTextFormat2:TextFormat = new TextFormat;
var font1:Font1 = new Font1;
var kids:Kids = new Kids;
var count:int = 0;
var finalScore:int = 0;
var score:Number = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
public function mainGame():void
{
trace("HI");
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChildAt(kids, 2);
kids.x = 310;
kids.y = 200;
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
}
public function bgScroll (e:Event)
{
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
e.target.addEventListener(Event.ENTER_FRAME, collision);
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var m:int=0; m< toyArray.length; m++)
{
(toyArray[m] as MovieClip).x -=BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int=0; j< toyArray.length; j++)
{
(toyArray[j] as MovieClip).x +=BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
} // End of BGScroll
public function collision (e:Event)
{
for (var l:int=0; l< toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x=100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
if (score == 200)
{
timerDone();
myTimer.stop();
}
}
}
public function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
public function arrayDrop(e:MouseEvent)
{
stopDrag();
}
public function resetGame(e:Event):void {
trace("CLICK");
countDown = 30;
myText.text = "0" + countDown.toString();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.reset();
removeChild(tryAgain);
myText2.visible = false;
score = 0;
}
public function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
if (countDown < 10)
{
myText.text = "0" + countDown.toString();
myText.x = 270;
displayText();
}
else if (countDown < 20 && countDown > 9)
{
myText.text = countDown.toString();
myText.x = 280;
displayText();
}
else
{
myText.text = countDown.toString();
myText.x = 270;
displayText();
}
} // end of countdown function
public function displayText():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 50, myText.y = 10;
myTextFormat.size = 50, myTextFormat.font = font1.fontName;
myText.setTextFormat(myTextFormat);
}
public function displayText2():void
{ myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
addChild(myText2);
myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;
myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;
myText2.setTextFormat(myTextFormat2);
}
public function timerDone(e:TimerEvent=null):void
{
if (countDown == 0)
{
count = 0;
finalScore = score;
}
else
{
count = (30) - (myTimer.currentCount);
finalScore = (count * 10) + (score);
}
myText.text = "GAME OVER!";
myText.x = 195;
displayText();
myText2.text = "Your score = " + (finalScore);
displayText2();
addChild(tryAgain);
tryAgain.x = 300;
tryAgain.y = 300;
tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
}
} // End of class
} //End of package
Nevermind ... solved it.
Easiest was was to change
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
To
function addToys(xpos:int, ypos:int)
{
stage.addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
And then, in the timerDone function I added thisif statement ...
for (var m = 0; m < toyArray.length; m++) {
if (stage.contains(toyArray[m])) {
stage.removeChild(toyArray[m]);
}
Worked a treat!

How to color each movieclip in a row

I will apreciate your help. I have 8 movieclips (square1-8) and a movieclip called plus1 on stage to handle the 8 movieclips. I want to click plus1 once and color square1, press plus1 for the second time and color square2 and so on. This is my code but it seems that there is something wrong because it color random. Can you please help me find what's wrong with it?
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
plus1.buttonMode=true;
var sximata:MovieClip = square1;
var myColorTransform:ColorTransform = new ColorTransform();
plus1.addEventListener(MouseEvent.CLICK, changeColour);
function changeColour(event:MouseEvent):void
{
if (contains(square1))
{
removeEventListener(MouseEvent.CLICK, onsquare1);
addEventListener(MouseEvent.CLICK, onsquare16);
}
if (contains(square2))
{
removeEventListener(MouseEvent.CLICK, onsquare2);
addEventListener(MouseEvent.CLICK, onsquare3);
}
if (contains(square3))
{
removeEventListener(MouseEvent.CLICK, onsquare3);
addEventListener(MouseEvent.CLICK, onsquare4);
}
if (contains(square4))
{
removeEventListener(MouseEvent.CLICK, onsquare4);
addEventListener(MouseEvent.CLICK, onsquare5);
}
if (contains(square5))
{
removeEventListener(MouseEvent.CLICK, onsquare5);
addEventListener(MouseEvent.CLICK, onsquare6);
}
if (contains(square6))
{
removeEventListener(MouseEvent.CLICK, onsquare6);
addEventListener(MouseEvent.CLICK, onsquare7);
}
if (contains(square7))
{
removeEventListener(MouseEvent.CLICK, onsquare7);
addEventListener(MouseEvent.CLICK, onsquare8);
}
myColorTransform.color = 0xBDB522;
sximata.transform.colorTransform = myColorTransform;
}
square1.addEventListener(MouseEvent.CLICK, onsquare1);
function onsquare1(e:MouseEvent):void {
sximata = square1;
}
square2.addEventListener(MouseEvent.CLICK, onsquare2);
function onsquare2(e:MouseEvent):void {
sximata = square2;
}
square3.addEventListener(MouseEvent.CLICK, onsquare3);
function onsquare3(e:MouseEvent):void {
sximata = square3;
}
square4.addEventListener(MouseEvent.CLICK, onsquare4);
function onsquare4(e:MouseEvent):void {
sximata = square4;
}
square5.addEventListener(MouseEvent.CLICK, onsquare5);
function onsquare5(e:MouseEvent):void {
sximata = square5;
}
square6.addEventListener(MouseEvent.CLICK, onsquare6);
function onsquare6(e:MouseEvent):void {
sximata = square6;
}
square7.addEventListener(MouseEvent.CLICK, onsquare7);
function onsquare7(e:MouseEvent):void {
sximata = square7;
}
square8.addEventListener(MouseEvent.CLICK, onsquare8);
function onsquare8(e:MouseEvent):void {
sximata = square8;
}
OK. My code looks like this now
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
plus1.buttonMode=true;
minus1.buttonMode=true;
var nextSquare:MovieClip = square1;
var squares:Array = [square1, square2, square3, square4, square5, square6, square7, square8];
var myColorTransform:ColorTransform = new ColorTransform();
plus1.addEventListener(MouseEvent.CLICK, changeColour);
function changeColour(event:MouseEvent):void
{
myColorTransform.color = 0x519596;
nextSquare.transform.colorTransform = myColorTransform;
var index = squares.indexOf(nextSquare);
if (index < squares.length - 1) {
nextSquare = squares[index + 1];
} else {
trace('we are done');
}
}
minus1.addEventListener(MouseEvent.CLICK, reversecolour);
function reversecolour(event:MouseEvent):void
{
nextSquare.transform.colorTransform = new ColorTransform();
var index = squares.indexOf(nextSquare);
if (index < squares.length - 1) {
nextSquare = squares[index - 1];
} else {
trace('we are done');
}
}
But i get this error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at _fla::MainTimeline/changeColour()
Not sure why you need so many event handlers. Can't you just keep the squares in an array and each time plus1 is clicked color and move to the next? Check out the code below.
plus1.buttonMode=true;
var nextSquare:MovieClip = square1;
var squares:Array = [square1, square2, square3, square4, square5, square6, square7, square8];
var myColorTransform:ColorTransform = new ColorTransform();
plus1.addEventListener(MouseEvent.CLICK, changeColour);
function changeColour(event:MouseEvent):void
{
myColorTransform.color = 0xBDB522;
nextSquare.transform.colorTransform = myColorTransform;
var index = squares.indexOf(nextSquare);
if (index < squares.length - 1) {
nextSquare = squares[index + 1];
} else {
trace('we are done');
}
}

ActionScript 3.0 AddChild Function Not Working

I have tested out my code and the addchild won't work. No errors are outputted.
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.*;
import flash.events.*;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.text.*;
public class Game extends flash.display.MovieClip{
public static const STATE_INIT:int = 10;
public static const STATE_PLAY:int = 20;
public static const STATE_END_GAME:int = 30;
public var gameState:int = 0;
public var score:int = 0;
public var chances:int = 5;
public var bg:MovieClip;
public var enemies:Array;
public var player:MovieClip;
public var level:Number = 0;
public var scoreLabel:TextField = new TextField
public var levelLabel:TextField = new TextField
public var chancesLabel:TextField = new TextField
public var scoreText:TextField = new TextField
public var levelText:TextField = new TextField
public var chancesText:TextField = new TextField
public const SCOREBOARD_Y:Number = 380
public function Game(){
addEventListener(Event.ENTER_FRAME, gameLoop);
bg = new BackImage();
addChild(bg);
scoreLabel.text = "Score:";
levelLabel.text = "level:";
chancesLabel.text = "Misses:";
scoreText.text = "0";
levelText.text = "1";
chancesText.text = "5";
scoreLabel.y = SCOREBOARD_Y;
levelLabel.y = SCOREBOARD_Y;
chancesLabel.y = SCOREBOARD_Y;
scoreText.y = SCOREBOARD_Y;
levelText.y = SCOREBOARD_Y;
chancesText.y = SCOREBOARD_Y;
scoreLabel.x = 5;
scoreText.x = 50;
chancesLabel.x = 105;
chancesText.x = 155;
levelLabel.x = 205;
levelText.x = 260
addChild(scoreLabel);
addChild(levelLabel);
addChild(chancesLabel);
addChild(scoreText);
addChild(levelText);
addChild(chancesText);
gameState = STATE_INIT;
}
public function gameLoop(e:Event):void{
switch(gameState){
case STATE_INIT:
initGame();
break;
case STATE_PLAY:
playGame();
break;
case STATE_END_GAME:
endGame();
break;
}
}
public function initGame():void{
score = 0;
chances = 5;
player = new playerImage();
enemies = new Array();
level = 1;
levelText.text = level.toString();
addChild(player);
player.startDrag(true,new Rectangle(0,0,550,400))
gameState = STATE_PLAY
}
public function playGame():void{
player.rotation += 15;
makeEnemies();
moveEnemies();
testCollisions();
testForEnd();
}
public function makeEnemies():void{
var chance:Number = Math.floor(Math.random()*100);
var tempEnemy:MovieClip;
if (chance < 2 + level) {
tempEnemy = new EnemyImage()
tempEnemy.speed = 3 + level;
tempEnemy.gotoAndStop(Math.floor(Math.random()*5)+1);
tempEnemy.y = 435;
tempEnemy.x = Math.floor(Math.random()*515)
addChild(tempEnemy);
enemies.push(tempEnemy);
}
}
public function moveEnemies():void{
var tempEnemy:MovieClip;
for (var i:int = enemies.length -1;i >= 0;i--){
tempEnemy = enemies[i];
tempEnemy.y -= tempEnemy.speed;
if (tempEnemy.y < -35){
chances -= 1;
chancesText.text = chances.toString();
enemies.splice(i,1);
removeChild(tempEnemy);
}
}
}
public function testCollisions():void {
var sound:Sound = new Pop();
var tempEnemy:MovieClip;
for (var i:int = enemies.length -1;i >= 0;i--){
tempEnemy = enemies[i];
if(tempEnemy.hitTestObject(player)){
score++;
scoreText.text = score.toString();
sound.play();
enemies.splice(i,1);
removeChild(tempEnemy);
}
}
}
public function testForEnd():void{
if(chances == 5){
gameState = STATE_END_GAME;
}
else if(score > level*20) {
level++;
levelText.text = level.toString();
}
}
public function endGame():void{
for(var i:int = 0; i< enemies.length; i++) {
removeChild(enemies[i]);
}
enemies = [];
player.stopDrag()
}
}
}
I have already tried adding this. and stage. in front of addchild and it still doesn't work.
This inside a file called Game.as
When you create your Game instance you need to add it as a child to the stage:
// I would probably rename the variable to 'game'
// 1st character of a variable name is conventionally lower-case
// var game:Game = new Game();
var Script:Game = new Game();
this.addChild(Script);

how can i make this papervision3d code work?

This code works but it does display the cube as the first code and the view, zoom and interactivity is different, so i wanted the cube to be displayed as in the first code with the same features.
first code is here: http://papervision2.com/10-advanced-interactivity/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
public class Main extends BasicView
{
protected var cube:Cube;
protected var interactiveMats:Array;
protected var materialsList:MaterialsList;
protected var targetrotationX:Number;
protected var targetrotationY:Number;
protected var targetrotationZ:Number;
protected var tweening:Boolean;
public function Main():void
{
super();
init();
}
protected function init():void
{
createChildren();
startRendering();
}
protected function createChildren():void
{
//Set the viewport to interactive
viewport.interactive = true;
//Create Materials:
materialsList = new MaterialsList();
interactiveMats = ["front", "back", "left", "right", "bottom", "top"];
var colorsArray:Array = [0x76b6f8, 0x4291e1, 0x1f73c8, 0xe77111, 0xe8914c, 0xfad2b2];
for (var i:int = 0; i < interactiveMats.length; i++)
{
//Create a color box so we can use our MouseEvents
var colorBox:Sprite = new Sprite();
colorBox.graphics.beginFill(colorsArray[i]);
colorBox.graphics.drawRect(0, 0, 100, 100);
colorBox.graphics.endFill();
colorBox.name = interactiveMats[i];
//Add a textField for reference
var textField:TextField = new TextField()
colorBox.addChild(textField)
textField.text = interactiveMats[i];
//Add a MouseEvent to the Sprite
colorBox.mouseChildren = false;
colorBox.addEventListener(MouseEvent.CLICK, onMovieMatClicked);
//Create the MovieMat
var movieMat:MovieMaterial = new MovieMaterial(colorBox, true, true);
movieMat.interactive = true;
movieMat.smooth = true;
materialsList.addMaterial(movieMat, interactiveMats[i]);
}
//Create Cube
cube = new Cube(materialsList, 100, 100, 100);
//Add cube to the scene
scene.addChild(cube);
}
protected function onMovieMatClicked(evt:MouseEvent):void
{
if (tweening)
{
// Let it rotate again
tweening = false;
}
else
{
switch(evt.target.name) {
case "front":
targetrotationX = 0;
targetrotationY = 180;
targetrotationZ = 0;
tweening = true;
break;
case "back":
targetrotationX = 0;
targetrotationY = 0;
targetrotationZ = 0;
tweening = true;
break;
case "left":
targetrotationX = 0;
targetrotationY = -90;
targetrotationZ = 0;
tweening = true;
break;
case "right":
targetrotationX = 0;
targetrotationY = 90;
targetrotationZ = 0;
tweening = true;
break;
case "top":
targetrotationX = -90;
targetrotationY = 0;
targetrotationZ = 0;
tweening = true;
break;
case "bottom":
targetrotationX = 90;
targetrotationY = 0;
targetrotationZ = 180;
tweening = true;
break;
}
}
}
override protected function onRenderTick(event:Event = null):void
{
super.onRenderTick(event);
if (tweening) {
// If a face has been clicked
if (camera.zoom <230) {
// If the camera isn't zoomed enough then zoom in a bit more:
camera.zoom += Math.sqrt(230-camera.zoom)/5;
}
// Test each rotation and rotate it towards the target rotation:
// X axis:
if (cube.rotationX < targetrotationX)
{
cube.rotationX += Math.sqrt(targetrotationX-cube.rotationX);
cube.rotationX = Math.round(cube.rotationX);
}
else if (cube.rotationX > targetrotationX)
{
cube.rotationX -= Math.sqrt(cube.rotationX-targetrotationX);
cube.rotationX = Math.round(cube.rotationX);
}
// Y axis:
if (cube.rotationY < targetrotationY)
{
cube.rotationY += Math.sqrt(targetrotationY-cube.rotationY);
cube.rotationY = Math.round(cube.rotationY);
}
else if (cube.rotationY > targetrotationY)
{
cube.rotationY -= Math.sqrt(cube.rotationY-targetrotationY);
cube.rotationY = Math.round(cube.rotationY);
}
// Z axis:
if (cube.rotationZ < targetrotationZ)
{
cube.rotationZ += Math.sqrt(targetrotationZ-cube.rotationZ);
cube.rotationZ = Math.round(cube.rotationZ);
}
else if (cube.rotationZ > targetrotationZ)
{
cube.rotationZ -= Math.sqrt(cube.rotationZ-targetrotationZ);
cube.rotationZ = Math.round(cube.rotationZ);
}
}
else
{
// If the camera is zoomed in, it shouldn't be now
if (camera.zoom > 200)
{
// So zoom out a bit.
camera.zoom -= Math.sqrt(camera.zoom-2)/5;
}
// Rotate the cube a bit:
cube.rotationX += 2;
cube.rotationY += 2;
// Make sure that we dont "wind up" the rotation
if (cube.rotationX>= 360) cube.rotationX = 0;
if (cube.rotationY>= 360) cube.rotationY = 0;
}
}
}
}
code2 taken from: http://papervision2.com/advanced-interactivity/
However the article was submitted, it parsed < to < and > to >.
Do a find/replace for each of those and the bulk of the errors should go away.