Calendar Actionscript 3 - actionscript-3

I need to make a calendar in AS3 that looks like this regularly:
and this when a date is clicked:
I have the basics down, I think but I don't know where to go from here and I cannot figure out what is wrong with my code to make the days work properly.
Main Code:
package code {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
private var days:Array = new Array();
public var selectedDay:Day = null;
public function Main() {
// constructor code
var across: int = 7;
for( var i:int = 0; i < 31; i++)
{
var row:int = Math.floor( i / across );
var col:int = i % across;
var d:Day = new Day();
addChild(d);
d.x = col * (d.width);
d.y = row * (d.height);
days.push(d);
d.addEventListener(MouseEvent.CLICK, onClick);
}
}
public function onClick(e:MouseEvent):void{
if (selectedDay == null){
trace("meow!");
days[1].gotoAndStop(2);
}
else if (selectedDay != null){
}
}
}
}
and the Day code:
package code {
import flash.display.MovieClip;
import flash.text.TextField;
public class Day extends MovieClip {
public var weekday_txt:TextField;
public var date_txt:TextField;
public function Day() {
// constructor code
for (var num:int = 0; num < 7; num++){
var weekDays:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
weekday_txt.text = weekDays[num];
//trace(weekDays[num]);
date_txt.text = ""+42;
}
}
}
}
Thanks for any help!

I rewrote your classes as it is easier than trying to explain the logical errors:
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip{
private var days:Array = new Array();
private var selectedDay:Day;
public function Main() {
stop();
days = new Array();
var weekDayTitles:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var across:int = 7;
for (var i:int=0; i<31; i++){
var row:int=Math.floor(i/across);
var col:int = i%7;
var d:Day = new Day();
addChild(d);
d.x = col * (d.width);
d.y = row * (d.height);
d.setWeekDay(weekDayTitles[col]);
d.setDate(""+(i+1));
days.push(d);
d.addEventListener(MouseEvent.CLICK, onClick);
}
}
public function onClick(e:MouseEvent):void{
if (selectedDay != null){
selectedDay.setAsUnSelected();
}
selectedDay = Day(e.target);
selectedDay.setAsSelected();
}
}// Class
And your Day class should look like this:
// make sure this class is linked to a movieclip
// in your library that has two frames
// frame 1 = "unselected"
// frame 2 = "selected";
// make sure to have 2 textfields in the linked symbol, one named : "weekdayTxt" and the other "dateTxt"
public class Day extends MovieClip{
public var weekday_txt:TextField;
public var date_txt:TextField;
public function Day() {
gotoAndStop(1);
weekday_txt = this.weekdayTxt;
date_txt = this.dateTxt;
}
public function setWeekDay(_day:String):void{
weekday_txt.text = _day;
}
public function setDate(_date:String):void{
day_txt.text = _date;
}
public function setAsSelected():void{
this.gotoAndStop(2);
}
public function setAsUnSelected():void{
this.gotoAndStop(1);
}
}// Class

Well, I think you do have the jist of it, but I notice that your onClick method is always targetting the second day of the array and setting it to goto frame 2, since it doesn't look like you ever initialize "selectedDay"...:
public function onClick(e:MouseEvent):void{
if (selectedDay == null){
trace("meow!");
days[1].gotoAndStop(2);
}
else if (selectedDay != null){
}
}
it might help to change the function to:
public function onClick(e:MouseEvent):void{
if (selectedDay != null){
selectedDay.gotoAndStop(1); //assuming frame 1 is an "unselected" state
} // you don't really need an else, if you are not going to do anything in it
selectedDay = e.target;
selectedDay.gotoAndStop(2);
}
public function getSelectedDayInfo():Object{
var _object = new Object();
_object.weekday = "nothing selected";
_object.date = "nothing selected";
if(selectedDay != null){
_object.weekday = selectedDay.weekday_txt.text;
_object.date = selectedDay.date_txt.text;
}
return (_object);
}
Hope that helps...

Related

Generating Random text on Collision AS3

I would like to generate some random words when an object hits another one.
I've tried it but only one word comes up and never changes again.
Also would it be better if I make a new class for the random texts or is including it in the Main class good enough? Thanks!
Here's my current code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends MovieClip {
public var cell:Cell;
public var group:Array;
public var gameTimer:Timer;
public var score:int = 0;
var array:Array = new Array ("Apples",
"Bananas",
"Grapes",
"Oranges",
"Pineapples"); //create an array of possible strings
var randomIndex:int = Math.random () * array.length;
//public var randomTxt:String;
public function Main() {
group = new Array();
gameTimer = new Timer (25);
gameTimer.addEventListener(TimerEvent.TIMER,moveCell);
gameTimer.start();
}
private function createCell(_x:Number = 0, _y:Number = 0):void {
var cell:Cell = new Cell(_x, _y);
group.push(cell);
addChild(cell);
}
function moveCell (timerEvent:TimerEvent):void {
if (Math.random() < 0.01) {
var randomX:Number = Math.random()*800;
var randomY:Number = Math.random()*600;
createCell(randomX, randomY);
}
for (var i:int = 0; i < group.length; i++)
{
var cell:Cell = Cell(group[i]);
if (cell.hitTestObject(island))
{
cell.parent.removeChild(cell);
group.splice(i,1);
score++;
txtWordDisplay.text = "Killed by" + array [randomIndex];
}
}
scoreOutPut.text = score.toString();
}
}
}
It's always the same word because you only set the randomIndex once at the beginning of your program.
A simple fix would be to update that value when the collision happens:
if (cell.hitTestObject(island))
{
...
randomIndex = Math.floor(Math.random () * array.length);
txtWordDisplay.text = "Killed by" + array [randomIndex];
}

how to insert sound to this code?

hello how to put sounds to this code so when tower firing bullet sound will show up together with it...i'm just nubie so i really need a help
can you help me how to put sounds in this code
this is the code
package Game
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import Gamess.BulletThree;
import Games.BulletTwo;
public class Tower_Fire extends MovieClip
{
private var isActive:Boolean;
private var range,cooldown,damage:Number;
public function Tower_Fire()
{
isActive = false;
range = C.TOWER_FIRE_RANGE;
cooldown = C.TOWER_FIRE_COOLDOWN;
damage = C.TOWER_FIRE_DAMAGE;
this.mouseEnabled = false;
}
public function setActive()
{
isActive = true;
}
public function update()
{
if (isActive)
{
var monsters = GameController(root).monsters;
if (cooldown <= 0)
{
for (var j = 0; j < monsters.length; j++)
{
var currMonster = monsters[j];
if ((Math.pow((currMonster.x - this.x),2)
+ Math.pow((currMonster.y - this.y),2)) < this.range)
{
//spawn new bullet
var bulletRotation = (180/Math.PI)*
Math.atan2((currMonster.y - this.y),
(currMonster.x - this.x));
var newBullet = new Bullet(currMonster.x,currMonster.y,
"fire",currMonster,this.damage,bulletRotation);
newBullet.x = this.x;
newBullet.y = this.y;
GameController(root).bullets.push(newBullet);
GameController(root).mcGameStage.addChild(newBullet);
this.cooldown = C.TOWER_FIRE_COOLDOWN;
break;
}
}
}
else
{
this.cooldown -= 1;
}
}
}
}
}
Firstly, you need to export sound for AS:
and then you can play sound from code:
var sound:Sound = new Track();
sound.play();
UPDATE:
if ((Math.pow((currMonster.x - this.x),2) + Math.pow((currMonster.y - this.y),2)) < this.range)
{
//spawn new bullet
var sound:Sound = new Track();
sound.play();
var bulletRotation = (180/Math.PI)*
Math.atan2((currMonster.y - this.y), (currMonster.x - this.x));
...

Actionscript 3: Error #1009: Cannot access a property or method of a null object reference. (parent)

This is a bit difficult to explain, but I will try.
In my Main() class, let's say I have the main movieclip (bg_image) being created, and also some lines that create an instance of another class. (look at the code below)
var route = Route(Airport.return_Route);
This route instance, is then purposed to dynamically add sprite-childs to the main background from the Main() class.
I have tried to to this with the following line:
new_flight = new Flight(coordinates)
Main.bg_image.addChild(new_flight);
This seem to work pretty fine. Let's switch focus to the new_flight instance. At the Flight class, this line trace(this.parent) would return "[object Image]".I would consider this successful so far since my intention is to add the new_flight as child to bg_image, which I guess is an "object image".
The problem, however, occurs when trying to delete the "new_flight"-instance. Obviously the ideal way of doing this would be through bg_image with removeChild.
But, when my script reaches this line: Main.bg_image.removeChild(this), my script stops and returns ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller., at that line. I have also tried replacing Main.bg_image.removeChild(this) with this.parent.removeChild(this), with no luck.
I think the solution may be to use some sort of "EventDispatching"-method. But I haven't fully understood this concept yet...
The code follows. Please help. I have no idea how to make this work...
Main class:
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import flash.display.DisplayObject;
import Airport;
public class Main extends Sprite
{
// ------------------------
// Buttons
public var create_new_route;
public var confirm_new_route;
// images
public static var bg_image:MovieClip;
public var airport:MovieClip;
//-------------------------------
public var routeArray:Array;
public static var airportDict:Dictionary = new Dictionary();
public static var collectedAirportArray:Array = new Array();
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
create_new_route = new Button();
create_new_route.label = "Create new route";
create_new_route.x = 220;
create_new_route.y = 580;
this.addChild(create_new_route)
create_new_route.addEventListener(MouseEvent.CLICK, new_route)
confirm_new_route = new Button();
confirm_new_route.label = "Confirm route";
confirm_new_route.x = 220;
confirm_new_route.y = 610;
this.addChild(confirm_new_route)
confirm_new_route.addEventListener(MouseEvent.CLICK, confirm_route)
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
bg_image = new Image();
addChild(bg_image);
S_USA["x"] = 180.7;
S_USA["y"] = 149.9;
S_USA["bynavn"] = "New York";
S_Norway["x"] = 423.7;
S_Norway["y"] = 76.4;
S_Norway["bynavn"] = "Oslo";
S_South_Africa["x"] = -26;
S_South_Africa["y"] = 146;
S_South_Africa["bynavn"] = "Cape Town";
S_Brazil["x"] = 226;
S_Brazil["y"] = 431.95;
S_Brazil["bynavn"] = "Rio de Janeiro";
S_France["x"] = 459.1;
S_France["y"] = 403.9;
S_France["bynavn"] = "Paris";
S_China["x"] = 716.2;
S_China["y"] = 143.3;
S_China["bynavn"] = "Beijing";
S_Australia["x"] = 809.35;
S_Australia["y"] = 414.95;
S_Australia["bynavn"] = "Sydney";
// ----------------------------------------------------
airportDict["USA"] = S_USA;
airportDict["Norway"] = S_Norway;
airportDict["South Africa"] = S_South_Africa;
airportDict["Brazil"] = S_Brazil;
airportDict["France"] = S_France;
airportDict["China"] = S_China;
airportDict["Australia"] = S_Australia;
for (var k:Object in airportDict)
{
var value = airportDict[k];
var key = k;
startList.addItem({label:key, data:key});
sluttList.addItem({label:key, data:key});
var airport:Airport = new Airport(key,airportDict[key]["bynavn"]);
airport.koordinater(airportDict[key]["x"], airportDict[key]["y"]);
collectedAirportArray.push(airport);
airport.scaleY = minScale;
airport.scaleX = minScale;
bg_image.addChild(airport);
}
// --------------------------------------------
// --------------------------------------------
// -----------------------------------------------
}
private function new_route(evt:MouseEvent):void
{
Airport.ROUTING = true;
}
public function confirm_route(evt:MouseEvent):void
{
Airport.ROUTING = false;
trace(Airport.return_ROUTE);
var route = new Route(Airport.return_ROUTE); // ***
this.addChild(route); // **
Airport.return_ROUTE = new Array();
}
}
}
Airport class:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
import flashx.textLayout.formats.Float;
public class Airport extends MovieClip
{
public static var ROUTING = false;
public static var return_ROUTE:Array = new Array();
//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
protected var navn:String;
protected var bynavn:String;
// ----------------------------------------------------------------------------
public function Airport(navninput, bynavninput)
{
this.bynavn = bynavninput;
this.navn = navninput;
this.addEventListener(MouseEvent.CLICK, clickHandler);
this.addEventListener(MouseEvent.MOUSE_OVER, hoverHandler);
}
public function zoomHandler():void
{
trace("testing complete");
}
public function koordinater(xc, yc)
{
this.x = (xc);
this.y = (yc);
}
private function clickHandler(evt:MouseEvent):void
{
trace(ROUTING)
if (ROUTING == true)
{
return_ROUTE.push([this.x, this.y])
}
}
private function hoverHandler(evt:MouseEvent):void
{
if (ROUTING == true)
{
this.alpha = 60;
this.width = 2*this.width;
this.height = 2*this.height;
this.addEventListener(MouseEvent.MOUSE_OUT, awayHandler);
}
}
private function awayHandler(evt:MouseEvent):void
{
this.width = 13;
this.height = 13;
}
}
}
Route class
package
{
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.MovieClip;
import Main;
public class Route
{
public var income:Number;
public var routePoints:Array;
private var routeTimer:Timer;
private var new_flight:Flight;
public function Route(route_array:Array)
{
this.routePoints = route_array
routeTimer = new Timer(2000);// 2 second
routeTimer.addEventListener(TimerEvent.TIMER, route_function);
routeTimer.start();
}
private function route_function(event:TimerEvent):void
{
for (var counter:uint = 0; counter < routePoints.length - 1; counter ++)
{
trace("Coords: ", routePoints[counter][0],routePoints[counter][1],routePoints[counter + 1][0],routePoints[counter + 1][1]);
new_flight = new Flight(routePoints[counter][0],routePoints[counter][1],routePoints[counter + 1][0],routePoints[counter + 1][1]);
Main.bg_image.addChild(new_flight);
var checkTimer:Timer = new Timer(15);// 1 second
checkTimer.addEventListener(TimerEvent.TIMER, check_function);
checkTimer.start();
function check_function(event:TimerEvent):void
{
if (new_flight.finished = true)
{
checkTimer.stop();
}
}
}
}
}
}
Flight class
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.events.TimerEvent;
import flash.utils.Timer;
import fl.controls.Button;
import flash.display.DisplayObject;
public class Flight extends MovieClip
{
public static var speed:uint = 1
public var finished:Boolean = false;
protected var absvector:Number;
protected var vector:Array;
protected var myTimer:Timer;
//protected var parentContainer:MovieClip;
protected var utgangspunkt_x;
protected var utgangspunkt_y;
protected var destinasjon_x;
protected var destinasjon_y;
protected var vector_x;
protected var vector_y;
public function Flight(utgangspunkt_x, utgangspunkt_y, destinasjon_x, destinasjon_y):void
{
addEventListener(Event.ADDED_TO_STAGE, init);
this.utgangspunkt_x = utgangspunkt_x;
this.utgangspunkt_y = utgangspunkt_y;
this.x = utgangspunkt_x;
this.y = utgangspunkt_y;
this.destinasjon_x = destinasjon_x + 10;
this.destinasjon_y = destinasjon_y + 10;
this.vector_x = Math.abs(this.destinasjon_x-this.utgangspunkt_x);
this.vector_y = Math.abs(this.destinasjon_y-this.utgangspunkt_y);
this.height = 20;
this.width = 20;
}
public function init(evt:Event):void
{
trace(this.parent)
if (utgangspunkt_x < destinasjon_x)
{
this.rotation = -(Math.atan((utgangspunkt_y-destinasjon_y)/(destinasjon_x-utgangspunkt_x)))/(Math.PI)*180;
}
else
{
this.rotation = 180-(Math.atan((utgangspunkt_y-destinasjon_y)/(destinasjon_x-utgangspunkt_x)))/(Math.PI)*180;
}
absvector = Math.sqrt(Math.pow((destinasjon_x - utgangspunkt_x),2) + Math.pow((utgangspunkt_y - destinasjon_y),2));
vector = [(destinasjon_x - utgangspunkt_x) / absvector,(utgangspunkt_y - destinasjon_y) / absvector];
stage.addEventListener(Event.ENTER_FRAME, movement)
}
private function movement(evt:Event):void
{
if (this.vector_x > this.vector_y)
{
if (destinasjon_x>utgangspunkt_x)
{
if (this.x < destinasjon_x)
{
this.x += speed*vector[0];
this.y -= speed*vector[1];
}
else
{
finished = true
Main.bg_image.removeChild(this)
}
}
else if (destinasjon_x<utgangspunkt_x)
{
if (this.x > destinasjon_x)
{
this.x += speed*vector[0];
this.y -= speed*vector[1];
}
else
{
finished = true
Main.bg_image.removeChild(this)
}
}
}
else
{
if (destinasjon_y>utgangspunkt_y)
{
if (this.y < destinasjon_y)
{
this.x += speed*vector[0];
this.y -= speed*vector[1];
}
else
{
finished = true
Main.bg_image.removeChild(this)
}
}
else if (destinasjon_y<utgangspunkt_y)
{
if (this.y > destinasjon_y)
{
this.x += speed*vector[0];
this.y -= speed*vector[1];
}
else
{
finished = true
Main.bg_image.removeChild(this)
}
}
}
}
}
}
I am confused what your asking and what your trying to do, but I will give it my best shot.
this.addchild(new_class2)
This line adds your object to the display list. Adding another object to the display is done the same way you added the first. Flash adds objects in the sequentially, so the objects you want in the back need to be declared and added first.
This is probably what you want:
var new_flight:Flight = new Flight();
this.addchild(new_flight);
Also you forgot your type declaration for Class2:
var new_class2:Class2 = new Class2();
Try replacing:
Main.bg_image.addChild(new_flight);
with
Main(this.parent).bg_image.addChild(new_flight);
... assuming the 'main' class you refer to is infact named 'Main' and it has a named instance called 'bg_image'
Please leave a comment if it works, I can explain in more detail what is actually happening here.
Why you are adding new_flight to bg_image?
Anyway, if you are adding new_flight to bg_image, then you have bg_image already declared as public static (which I do not recommend),
Try removing the flight in your Flight class it-self like so,
Main.bg_image.removeChild(this) // *
You can also use Event Dispatching instead of declaring bg_image as static.
If you want to remove flight then dispatch event from Flight class to Route class and there you again dispatch event to main class.
And, inside main class capture this event and access the MovieClip.

Error 1006: Not a function

I am getting the error in the title when trying to access a method in another class. I have the main class, ZombieBots, which is linked to a movie clip of the same name. I then have 3 more movie clips which all get added to the ZombieBots clip during runtime, and each of these have their own classes.
When I attempt to access a method within the ZombieBots class from one of the other 3 classes, I get error 1006.
The function I am attempting to access in the ZombieBots class, that cannot be accessed:
package {
import flash.events.*;
import flash.display.MovieClip;
import flash.geom.Rectangle;
public class ZombieBots extends MovieClip{
private var pLives:int;
private var pScore:int;
private var pSkill:int;
private var pItems:int;
private var characterMC:Character;
private var cGameObjs:Array;
public function ZombieBots() {
/*cGameObjs = new Array();
addCharacter();
addItems();
addBots();
pLives = 5 - pSkill;
pScore = 0;
pItems = pSkill + 5;*/
resetGame();
}
private function addCharacter():void{
trace("Adding the character");
if (!characterMC){
var myBorder:Rectangle = new Rectangle(35,35,600,480);
var myXY:Array = [38, 400];
var myChar:int = Math.ceil(Math.random()*3);
var myKeys:Array = [37,39,38,40];
var myDistance:int = myChar * 3;
characterMC = new Character(myBorder, myXY, myKeys, myChar, myDistance);
addChild(characterMC);
}
else{
characterMC.x = 38;
characterMC.y = 510;
characterMC.gotoAndStop(pSkill);
}
}
private function addItems():void{
trace("yeah boi");
var mySkill:int = Math.ceil(Math.random() *3);
var myMaxItems:int = mySkill + 5;
trace(mySkill);
trace(myMaxItems);
trace(this);
for (var i:int = 0; i < myMaxItems; i++){
var thisItem:Item = new Item(this, characterMC, mySkill);
thisItem.name = "item" + i;
cGameObjs.push(thisItem);
addChild(thisItem);
}
pSkill = mySkill;
updateScores();
}
private function addBots():void{
trace("adding the bots bra");
var myBorder:Rectangle = new Rectangle(100,100,400,350);
var mySkill:int = Math.ceil(Math.random()*3);
var myMaxBots:int = mySkill +10;
for (var i:int = 0; i < myMaxBots; i++){
var thisBot:Bot = new Bot(myBorder, characterMC, mySkill);
thisBot.name = "bot" + i;
cGameObjs.push(thisBot);
addChild(thisBot);
}
}
private function updateScores():void{
scoreDisplay.text = String(pScore);
itemsDisplay.text = String(pItems);
livesDisplay.text = String(pLives);
msgDisplay.text = "Orc Invasion";
}
public function updateLives(myBot:MovieClip):void{
trace("update lives");
pLives--;
pScore -= myBot.getPts();
var myIndex:int = cGameObjs.indexOf(myBot);
cGameObjs.splice(myIndex, 1);
if (pLives > 0){
updateScores();
}
else{
gameOver(false);
}
}
public function updateItems(myItem:MovieClip):void{
trace("update items");
pItems--;
pScore += myItem.getPts();
var myIndex:int = cGameObjs.indexOf(myItem);
cGameObjs.splice(myIndex, 1);
if (pItems > 0){
updateScores();
}
else{
gameOver(true);
}
}
private function gameOver(bool:Boolean):void{
trace("Game over dawg");
updateScores();
if(bool){
msgDisplay.text = "Good job buddy";
}
else{
msgDisplay.text = "You suck dawg";
}
removeLeftovers();
}
private function resetGame():void{
playAgainBtn.visible = false;
playAgainBtn.removeEventListener(MouseEvent.CLICK,playAgain);
cGameObjs = new Array();
addCharacter();
addItems();
addBots();
pLives = 5 - pSkill;
pScore = 0;
pItems = pSkill + 5;
updateScores();
}
private function playAgain(evt:MouseEvent):void{
resetGame();
}
private function removeLeftovers():void{
trace("Removing leftover items and bots");
for each(var myObj in cGameObjs){
myObj.hasHitMe();
myObj = null;
}
playAgainBtn.visible = true;
playAgainBtn.addEventListener(MouseEvent.CLICK, playAgain);
}
}
}
and this is the class where I am attempting to access this function within one of the other 3 classes:
package {
import flash.display.MovieClip;
import flash.events.*;
import ZombieBots;
public class Item extends MovieClip{
private var cNumItem:int;
private var cNumPts:int;
private var characterMC:MovieClip;
private var ZombieBot:ZombieBots;
public function Item(myZB:ZombieBots, myChar:MovieClip, mySkill:int=1) {
ZombieBot = myZB;
cNumItem = Math.ceil(Math.random() * (mySkill * 3 + 1));
characterMC = myChar;
this.addEventListener(Event.ADDED_TO_STAGE,initItem);
addEventListener(Event.ENTER_FRAME, checkCollision);
}
private function initItem(evt:Event):void{
this.gotoAndStop(cNumItem);
cNumPts = cNumItem * 25;
setPosition();
this.removeEventListener(Event.ADDED_TO_STAGE,initItem);
}
private function setPosition():void{
this.x = (Math.ceil(Math.random() * 10)*50);
this.y = (Math.ceil(Math.random()*10)*35);
}
private function checkCollision(evt:Event){
if (characterMC.hitTestObject(this)){
ZombieBot.updateItems(this);
hasHitMe();
}
}
public function hasHitMe():void{
trace("remove");
removeEventListener(Event.ENTER_FRAME, checkCollision);
this.parent.removeChild(this);
}
public function getPts():int{
return cNumPts;
}
}
}
can anyone help?
updateItems is not a MovieClip method. It's a method of your ZombieBots class.
Casting your ZombieBots instance (which I am assuming is root) as a MovieClip, will only allow you to use it's class methods or methods it has inherited.
Try this :
var zombieBotsInstance:ZombieBots = root as ZombieBots;
zombieBotsInstance.updateItems(this);

AS3 > Starting class on certain frame? TypeError :Error #1006

Im trying to create a simple memory game i have 3 frames Intro ,Main Game , End however every time i click the "start button" to jump to frame 2 i keep getting this error;
TypeError: Error #1006: Play_AnimalCardGame is not a function.
at AnimalCardGame/frame2()
at flash.display::MovieClip/gotoAndStop()
at AnimalCardGame/startGame()
My .AS
package{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
public class Play_AnimalCardGame extends MovieClip
{
private static const boardWidth:uint =4;
private static const boardHeight:uint =3;
private static const cardVSpace:Number=100;
private static const cardHSpace:Number=15;
private static const offSetX:Number=115;
private static const offSetY:Number=155;
public function Play_AnimalCardGame ():void
{
var cardDeck:Array = new Array();
for ( var i:uint=0;i<boardWidth*boardHeight/2;i++){
cardDeck.push(i);
cardDeck.push(i);
}
for(var x:uint=0; x<boardWidth ; x++){
for(var y:uint=0; y<boardHeight;y++){
var aCard:Card = new Card();
aCard.stop();
aCard.x = x*offSetX+cardVSpace;
aCard.y = y*offSetY+cardHSpace;
var randomCard:uint = Math.floor(Math.random()*cardDeck.length);
aCard.cardface= cardDeck[randomCard];
cardDeck.splice(randomCard,1);
aCard.gotoAndStop(1);
aCard.addEventListener(MouseEvent.CLICK,clickCard);
addChild(aCard);
cardLeft++;
}
}
}
private var firstPick:Card;
private var secondPick:Card;
private var cardLeft;
private static const pointHit:int =100;
private static const pointMiss:int = -5;
private var startscore =0;
var startTime:uint;
var time:uint;
public function clickCard(event:MouseEvent){
var pickedCard:Card = (event.currentTarget as Card);
if(firstPick == null){
firstPick =pickedCard;
firstPick.gotoAndStop(pickedCard.cardface+2);
}
else if (firstPick ==pickedCard){
firstPick.gotoAndStop(1);
firstPick=null;
}
else if (secondPick ==null){
secondPick= pickedCard;
secondPick.gotoAndStop(pickedCard.cardface+2);
if (firstPick.cardface == secondPick.cardface){
startscore +=pointHit;
cardLeft-=2;
removeChild(firstPick);
removeChild(secondPick);
txtscore.text= String(startscore);
firstPick = null;
secondPick=null;
}
else{
firstPick.gotoAndStop(1);
secondPick.gotoAndStop(1);
startscore +=pointMiss;
txtscore.text= String(startscore);
secondPick=null;
firstPick = pickedCard;
firstPick.gotoAndStop(pickedCard.cardface+2);
}
}
if(cardLeft==0){
gotoAndStop("gameover");
}
}
public function showTimer(event:Event)
{
startTime = getTimer();
time=0;
time = getTimer()- startTime;
txtTime.text = clockTime(time);
}
public function clockTime(ms:int){
var seconds:int = Math.floor(ms/1000);
var minutes:int = Math.floor(seconds/60);
seconds -=minutes *60;
var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
return timeString;
}
}
From what i can there are no problems but then again in still only learning
It looks like you've defined the class name as
Play_AnimalCardGame
but then you define the constructor as
AnimalCardGame
Start by setting the constructor name to the same as the class name