i'm just getting started programming and wanted to know how to make the cards i made in actions script a random color with a MouseEvent. I have to do this for a a school project in Animate (adobe). (This is also the first time using this website). This is my code:
This is the card.as:
package {
import flash.display.MovieClip;
import flash.geom.ColorTransform;
import flash.text.TextField;
public class card extends MovieClip {
//praperties
public var cardNumber:int;
public var cardColor:ColorTransform;
public var cardType:String;
public var minNum:Number;
public var maxNum:Number;
public var textBox:TextField;
//constructor
public function card() {
cardType = randomCardType();
cardNumber = 5;
cardColor = randomcardColor();
trace(cardColor);
trace(cardType);
transform.colorTransform = cardColor;
textBox = new TextField();
addChild(textBox);
textBox.text = String(cardType + "\n" + cardNumber);
textBox.width = 100;
textBox.height = 100;
textBox.border = true;
}
//methods
public function printinfo() :void {
trace("cardNumber: '" + cardNumber + "'");
}
public function randomcardColor():ColorTransform {
var kleur:ColorTransform = new ColorTransform();
kleur.color = (Math.random() * 0xFFFFFF);
return kleur;
}
public function randomCardType(){
var Name:Array = ["Knight","Archer","Wizard","Priest"];
var randomNum:int = Math.floor(Math.random()* Name.length);
return Name[randomNum];
}
function randomRange(minNum:Number, maxNum:Number):Number {
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
public function checkcardColor (otherColor) {
if (cardColor == otherColor) {
return true;
}
else {
return false;
}
}
}
}
and this is the cardgame.as:
package {
import flash.display.MovieClip;
import fl.motion.Color;
public class cardgame extends MovieClip {
//proparties
public var cardA:card;
public var cardB:card;
public var cardC:card;
public var cardD:card;
// constructor code
public function cardgame() {
cardA = new card();
cardA.x = 100;
cardA.y = 100;
cardA.width = 100;
cardA.height = 100;
this.addChild(cardA);
cardB = new card();
cardB.x = 450;
cardB.y = 100;
cardB.width = 100;
cardB.height = 100;
this.addChild(cardB);
cardC = new card();
cardC.x = 450;
cardC.y = 300;
cardC.width = 100;
cardC.height = 100;
this.addChild(cardC);
cardD = new card();
cardD.x = 100;
cardD.y = 300;
cardD.width = 100;
cardD.height = 100;
this.addChild(cardD);
showMeTheWorld();
}
//methods
public function showMeTheWorld() {
cardA.printinfo();
cardB.printinfo();
cardC.printinfo();
cardD.printinfo();
}
}
}
put following, inside constructor (public function card() {/*Here*/})
this.addEventListener(MouseEvent.CLICK, cardClicked);
also you had done the random color function
so put it in your card class
public function cardClicked(e:MouseEvent):void {
MovieClip(e.target).colorTransform = randomcardColor();
}
don't forget importing MouseEvent
read more about EventListener
I'm trying to compile my 2D game to test it and I'm getting this error: \Main.as, Line 32 1136: Incorrect number of arguments. Expected 2.
Line 32 contains this code var enemy:Enemy = new Enemy(null);
Any help is always appreciated, thanks everyone.
Code associated with the error (I can post the rest if needed):
Main.as
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
public var player:Player;
public var enemy:Enemy;
public var bulletList:Array = [];
public var mousePressed:Boolean = false; //keeps track of whether the mouse is currently pressed down
public var delayCounter:int = 0; //this adds delay between the shots
public var delayMax:int = 7; //change this number to shoot more or less rapidly
public var enemies:Array = [];
public function Main():void
{
player = new Player(stage, 320, 240);
stage.addChild(player);
//stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true); //remove this
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
for(var numBaddies=0; numBaddies<6;numBaddies++){
var enemy:Enemy = new Enemy(null);
enemy.x = numBaddies*50;
enemy.y = numBaddies*50
stage.addChild(enemy);
enemies.push(enemy);
}
}
public function loop(e:Event):void
{
if(mousePressed) // as long as the mouse is pressed...
{
delayCounter++; //increase the delayCounter by 1
if(delayCounter == delayMax) //if it reaches the max...
{
shootBullet(); //shoot a bullet
delayCounter = 0; //reset the delay counter so there is a pause between bullets
}
}
if(bulletList.length > 0)
{
for(var i:int = bulletList.length-1; i >= 0; i--)
{
bulletList[i].loop();
}
}
/*for(var h = 0; h<bulletList.length; ++h)
{
if(bulletList[h].hitTestObject(this)){
trace("player hit by baddie " + h);
}
}*/
for(var u:int=0; u<enemies.length; u++) {
Enemy(enemies[u]).moveTowards(player.x, player.y);
}
}
public function mouseDownHandler(e:MouseEvent):void //add this function
{
mousePressed = true; //set mousePressed to true
}
public function mouseUpHandler(e:MouseEvent):void //add this function
{
mousePressed = false; //reset this to false
}
public function shootBullet():void //delete the "e:MouseEvent" parameter
{
var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation, enemies);
bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true);
bulletList.push(bullet);
stage.addChild(bullet);
}
public function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved);
bulletList.splice(bulletList.indexOf(e.currentTarget),1);
}
}
}
Enemy.as
package {
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
public var bullets:Array;
public var stageRef:Stage;
private var enemyPositionX, enemyPositionY,xDistance,yDistance,myRotation:int;
public function Enemy(stageRef:Stage, bulletList:Array) {
// constructor code
bullets = bulletList;
this.stageRef = stageRef;
}
public function moveTowards(playerX:int, playerY:int){
xDistance = this.x - playerX;
yDistance = this.y - playerY;
myRotation = Math.atan2(yDistance, xDistance);
this.x -= 3 * Math.cos(myRotation);
this.y -= 3 * Math.sin(myRotation);
}
private function removeSelf():void
{
//removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
As you said, error is pointing to:
var enemy:Enemy = new Enemy(null);
and Enemy constructor is:
public function Enemy(stageRef:Stage, bulletList:Array){
So you are missing 2nd parameter - bulletList.
I got this code for recording users sound:
public class Main extends Sprite
{
private var mic:Microphone;
private var waveEncoder:WaveEncoder = new WaveEncoder();
private var recorder:MicRecorder = new MicRecorder(waveEncoder);
private var recBar:RecBar = new RecBar();
private var tween:Tween;
private var fileReference:FileReference = new FileReference();
public function Main():void
{
recButton.stop();
activity.stop();
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 100;
mic.setLoopBack(true);
mic.setUseEchoSuppression(true);
Security.showSettings("2");
addListeners();
}
private function addListeners():void
{
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
recorder.addEventListener(RecordingEvent.RECORDING, recording);
recorder.addEventListener(Event.COMPLETE, recordComplete);
activity.addEventListener(Event.ENTER_FRAME, updateMeter);
}
private function startRecording(e:MouseEvent):void
{
if (mic != null)
{
recorder.record();
e.target.gotoAndStop(2);
recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);
addChild(recBar);
tween = new Tween(recBar,"y",Strong.easeOut, -recBar.height,0,1,true);
}
}
private function stopRecording(e:MouseEvent):void
{
recorder.stop();
mic.setLoopBack(false);
e.target.gotoAndStop(1);
recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);
}
private function updateMeter(e:Event):void
{
activity.gotoAndPlay(100 - mic.activityLevel);
}
private function recording(e:RecordingEvent):void
{
var currentTime:int = Math.floor(e.time / 1000);
recBar.counter.text = String(currentTime);
if (String(currentTime).length == 1)
{
recBar.counter.text = "00:0" + currentTime;
}
else if (String(currentTime).length == 2)
{
recBar.counter.text = "00:" + currentTime;
}
}
private function recordComplete(e:Event):void
{
fileReference.save(recorder.output, "recording.wav");
}
}
I want to replace mouse events with timer event. If lasted time == 5 then start recording and after
10 seconds stop recording. I confused where add my timer code something like this:
var myIntrotime:Timer = new Timer(1000,5);
myIntrotime.addEventListener(TimerEvent.TIMER, startIntroTime);
myIntrotime.start();
var SecondsElapsed:Number = 1;
function startIntroTime(event:TimerEvent):void
{
if (SecondsElapsed==5)
{
//start recording
//start another timer2 and if timer2 finished stop recording
}
SecondsElapsed++;
}
Could someone help me?
You can do better if you employ flash.utils.setTimeout() to make a delayed call. This makes all the dirty work with timers for you.
setTimeout(startIntroTime,5000)
function startIntroTime():void
{
//start recording
setTimeout(stopRecording,10000);
}
The manual on setTimeout()
I'm trying to place a dynamic generated text on a bezier curve - the text can be up to 12 charcters long, so it will need to adjust to the center.
I can't find any attempt to do this on google.
my only guess is reading a bezier curves xy, then placing the textfield on that, but how would i get the Textfield to fit to the curve?
Thanks!
Marcus
If your curve is an arc or a circle I have just the right thing for you :p
I made two classes some time ago to handle this here they come:
package com.display{
import flash.display.Sprite;
import utils.Utils;
public class PathText extends Sprite
{
private var _text:String;
private var _chars:Array;
private var _startAngle:Number;
private var _stopAngle:Number;
private var _radius:Number;
public function PathText(__text:String, __radius:Number)
{
super();
this.mouseChildren = false;
this.mouseEnabled = false;
_text = __text;
_startAngle = 0;
_radius = __radius;
_chars = new Array();
init();
}
private function init():void
{
for(var i:int = 0; i < _text.length; i++)
{
var char:Char = new Char(_text.charAt(i));
_chars.push(char);
}
drawArc(_radius);
}
public function drawArc(rad:Number):void
{
var lastAngle:Number = 0;
for(var i:int = 0; i < _chars.length; i++)
{
var angle:Number = 2 * Math.sin((_chars[i].width/2)/rad);
_chars[i].rotation = radiansToDegrees(lastAngle) + 90;
_chars[i].x = rad * Math.cos(lastAngle);
_chars[i].y = rad * Math.sin(lastAngle);
lastAngle += angle;
addChild(_chars[i]);
}
_stopAngle = radiansToDegrees(lastAngle);
}
public function destroy():void
{
for(var i:int = _chars.length; i >= 0; i--)
{
removeChild(_chars[i]);
_chars.pop().destroy();
}
}
public function get startAngle():Number
{
return _startAngle;
}
public function set startAngle(value:Number):void
{
_startAngle = value;
}
public function get stopAngle():Number
{
return _stopAngle;
}
public function set stopAngle(value:Number):void
{
_stopAngle = value;
}
private function radiansToDegrees(radians:Number):Number
{
return radians * 180 / Math.PI;
}
}
}
and the Char class:
package com.display
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class Char extends Sprite
{
private var _str:String;
public function Char(__str:String)
{
super();
_str = __str;
this.mouseChildren = false;
this.mouseEnabled = false;
init();
}
private function init():void
{
var _tf:TextField = new TextField();
_tf.defaultTextFormat = new TextFormat("Some Font here", 15, 0xffffff);
_tf.embedFonts = true;
_tf.mouseEnabled = false;
_tf.selectable = false;
_tf.text = _str;
_tf.autoSize = TextFieldAutoSize.LEFT;
_tf.width = _tf.textWidth;
addChild(_tf);
}
public function destroy():void
{
this.removeChildAt(0);
}
public function get str():String
{
return _str;
}
public function set str(value:String):void
{
_str = value;
}
}
}
Tell me if it works for you...
cheers!
I need to use stage.width/height in my CustomClass so I found some topics about it.
if (stage)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
But in my case it won't work because it isn't document class, I think. Any other solution?
UPDATE:
CLASS CODE
package
{
import fl.transitions.Tween;
import fl.motion.easing.*;
import flash.filters.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.display.*;
public class Wyjazd extends MovieClip
{
public function Wyjazd(ar:Array=null,firma:Object=null,kontakt:Object=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null)
{
if (stage)
{
//The stage reference is present, so we're already added to the stage
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(ar:Array,firma:Object=null,kontakt=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null):void
{
//Zmienne "globalne" dla funkcji
var time:Number;
var wciecie:Number;
var wciecie2:Number;
var offset:Number = 15.65;
var offset2:Number = 20;
var posX:Array = new Array(12);
var posY:Array = new Array(12);
var spr:Array = new Array(12);
var targetLabel:String;
var wybranyOb:Object = ar[0];
var names:Array = new Array('Szkolenie wstępne BHP','Szkolenie okresowe BHP','Szkolenie P.Poż','Kompleksowa obsługa P.Poż','Pomiar środowiska pracy','Szkolenie z udzielania pierwszej pomocy','Ocena ryzyka zawodowego','Przeprowadzanie postępowań po wypadkowych','Przeprowadzanie audytów wewnętrznych ISO','Hałas w środowisku komunalnym','Medycyna pracy','Szkolenia dla kierowców');
//Pobieranie pozycji
for (var i:Number = 0; i<ar.length; i++)
{
posX[i] = ar[i].x;
posY[i] = ar[i].y;
}
//Filtry
function increaseBlur(e:MouseEvent,docPos:Number):void
{
var myBlur:BlurFilter =new BlurFilter();
myBlur.quality = 3;
myBlur.blurX = 10;
myBlur.blurY = 0;
}
//Funkcje
function startPos():void
{
time = 0.2;
for (var i:Number = 0; i<ar.length; i++)
{
//if (wybranyOb.name == ar[i].name)
//{
//var wybranyPos:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],0.01,true);
//wybranyPos = new Tween(ar[i],"y",Linear.easeOut,-30,posY[i],time,true);
//}
//else
//{
var position:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
position = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
//}
//time = 0.2;
}
position = new Tween(naglowek,"x",Linear.easeOut,naglowek.x,2000,time,true);
position = new Tween(tekst,"x",Linear.easeOut,tekst.x,2000,time,true);
position = new Tween(dane,"x",Linear.easeOut,dane.x,2000,0.25,true);
}
//Nasłuchy
oferta.addEventListener(MouseEvent.CLICK, wyskokOferta);
oferta.addEventListener(MouseEvent.MOUSE_OVER,glowOferta);
oferta.addEventListener(MouseEvent.MOUSE_OUT,unglowOferta);
kontakt.addEventListener(MouseEvent.CLICK,wyskokKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OVER,glowKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OUT,unglowKontakt);
firma.addEventListener(MouseEvent.CLICK,wyskokFirma);
firma.addEventListener(MouseEvent.MOUSE_OVER,glowFirma);
firma.addEventListener(MouseEvent.MOUSE_OUT,unglowFirma);
function glowFirma(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
firma.filters = [myGlow];
}
function unglowFirma(e:MouseEvent):void
{
firma.filters = [];
}
function glowKontakt(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
kontakt.filters = [myGlow];
}
function unglowKontakt(e:MouseEvent):void
{
kontakt.filters = [];
}
function glowOferta(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
oferta.filters = [myGlow];
}
function unglowOferta(e:MouseEvent):void
{
oferta.filters = [];
}
function wyskokKontakt(e:MouseEvent):void
{
startPos();
var tweenKontakt = new Tween(dane,"x",Linear.easeOut,2000,350,0.25,true);
}
function wyskokFirma(e:MouseEvent):void
{
startPos();
trace("Firma");
}
function wyskokOferta(e:MouseEvent):void
{
time = 0.2;
wciecie = 15.65;
wciecie2 = 20.05;
for (var i:Number = 0; i < ar.length; i++)
{
var tween:Tween = new Tween(ar[i],"x",Sine.easeOut,ar[i].x,oferta.x + wciecie,time,true);
tween = new Tween(ar[i],"y",Sine.easeOut,ar[i].y,oferta.y + wciecie2,time,true);
ar[i].addEventListener(MouseEvent.CLICK,onClick);
spr[i] = i;
time += 0.02;
wciecie += offset;
wciecie2 += offset2;
}
}
function onClick(e:MouseEvent)
{
startPos();
time = 0.2;
var k:Number = 0;
targetLabel = e.currentTarget.name;
for (var i:Number = 0; i < ar.length; i++)
{
if (targetLabel==ar[i].name)
{
//wybranyOb = ar[i];
var tween:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
tween = new Tween(naglowek,"x",Linear.easeOut,2000,60,0.2,true);
tween = new Tween(tekst,"x",Linear.easeOut,2000,500,0.25,true);
naglowek.text = names[i];
}
else
{
var tween1:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween1 = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
}
//time += 0.02;
}
}
}
}
}
Hope it will helps.
I expect you are getting an error from the init function which expects lots of parameters but might just get one Event. It helps when you post here if you post the compile or runtime errors you are getting along with source code.
I think this should work for you, I've made a rough version which you can learn from and apply to your own class
public class CustomClass extends MovieClip
{
protected var _company:String;
protected var _data:Object;
public function CustomClass( company:String='', data:Object=null )
{
_company = company;
_data = data;
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//do something with _data
//do something with _company
}
}
Hopefully you can see the concept here, place your constructor variables in class variables when you create the class, then if on the stage call init() which uses those class variables or add an event listener which will call init(passing in an event this time) and then use the same class variables to do what you want.
Note how I remove the event listener when it isn't needed any more as well.