ForumsProgramming Forumhelp

9 4433
alex57834
offline
alex57834
88 posts
445

GameClass Working
Avatar Working
TypeError: Error #1010: A term is undefined and has no properties.
at GameClass()
at Document()

package {

import flash.display.MovieClip;
import flash.events.*;

public class Document extends MovieClip{

public var game:GameClass;

public function Document()
{
game = new GameClass(stage);
game.addEventListener("GAMEOVER", onGameFinish);
addChild(game);
}

public function onGameFinish(GameEvent)
{
game.removeEventListener("GAMEOVER", onGameFinish);

trace("OnFinishFunction working"

}
}

}


package {

import flash.display.*;
import flash.events.*;
import flash.text.*;

public class GameClass extends MovieClip {

var avatarGuy:avatar;
var fallFruit:FallingObjects;
var points:Number;

public function GameClass(stage)
{
trace("GameClass Working"

points = 0;

var avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

var fallFruit = new FallingObjects();
addChild(fallFruit);

var countdownDisplayMain:CountdownDisplayMain = new CountdownDisplayMain();
countdownDisplayMain.x = 100;
countdownDisplayMain.y = 350;
addChild(countdownDisplayMain);

var score:scoreBox = new scoreBox();
score.x = 250;
score.y = 350;
addChild(score);

score.digits.text = points.toString();

addEventListener(Event.ENTER_FRAME, loop);

function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
var fruit = fallFruit.getChildAt(i);
if(fruit is apple)
{
scoreSystem(10);
}
if(fruit is watermelon)
{
scoreSystem(100);
}
if(fruit is orange)
{
scoreSystem(5)
}
if(fruit is strawberry)
{
scoreSystem(15);
}
fallFruit.removeChild(fallFruit.getChildAt(i));
}
}
}

function scoreSystem(n):void
{
points = points + n;
score.digits.text = points.toString();
}
}

}

  • 9 Replies
weirdlike
offline
weirdlike
1,309 posts
28,720

im not sure if you copied and pasted correctly in your GameClass I count 11-{ and only 10-}

other than that it looks ok, I suggest commenting out a few lines then testing

ex.
package {

import flash.display.*;
import flash.events.*;
import flash.text.*;

public class GameClass extends MovieClip {

var avatarGuy:avatar;
var fallFruit:FallingObjects;
var points:Number;

public function GameClass(stage)
{
trace("GameClass Working&quot;

points = 0;

var avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

var fallFruit = new FallingObjects();
addChild(fallFruit);

var countdownDisplayMain:CountdownDisplayMain = new CountdownDisplayMain();
countdownDisplayMain.x = 100;
countdownDisplayMain.y = 350;
addChild(countdownDisplayMain);

var score:scoreBox = new scoreBox();
score.x = 250;
score.y = 350;
addChild(score);

score.digits.text = points.toString();

addEventListener(Event.ENTER_FRAME, loop);

function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
var fruit = fallFruit.getChildAt(i);
// if(fruit is apple)
// {
// scoreSystem(10);
// }
// if(fruit is watermelon)
// {
// scoreSystem(100);
// }
// if(fruit is orange)
// {
// scoreSystem(5)
// }
// if(fruit is strawberry)
// {
// scoreSystem(15);
// }

fallFruit.removeChild(fallFruit.getChildAt(i));
}
}
}

function scoreSystem(n):void
{
points = points + n;
score.digits.text = points.toString();
}
}

}

run the code like this if you still get the error then you know its not any of these lines, you can then proceed to comment out more lines until it works correctly. Once you find the issue then you will more understand what you need to do to remedy it

alex57834
offline
alex57834
88 posts
445

Yeah thanks i found the problem and fixed it.

alex57834
offline
alex57834
88 posts
445

How would I add a function where the finishing score is shown on a game over screen

package {

import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.*;

public class CountdownDisplayMain extends MovieClip {

var time:Number;
var timeTimer:Timer;

public function CountdownDisplayMain()
{
time = 60;

CountdownClockDisplay.text = time.toString();

timeTimer = new Timer(1000);
timeTimer.addEventListener(TimerEvent.TIMER,countdown);
timeTimer.start();

function countdown(event):void
{
time -= 1;
CountdownClockDisplay.text = time.toString();

if(time == 0)
{
timeTimer.stop();
timeTimer.removeEventListener(TimerEvent.TIMER, countdown);

trace("dispatchingEvent&quot;
dispatchEvent(new Event("GAMEOVER", true));
}
else
{
time == 0;
CountdownClockDisplay.text = time.toString();
}

}

}
}
}



package {

import flash.display.MovieClip;
import flash.events.*;

public class Document extends MovieClip{

public var game:GameClass;

public function Document()
{
game = new GameClass(stage);
game.addEventListener("GAMEOVER", onGameFinish);
addChild(game);
}

public function onGameFinish(GameEvent)
{
game.removeEventListener("GAMEOVER", onGameFinish);

trace("OnFinishFunction working&quot;

}
}

}


package {

import flash.display.*;
import flash.events.*;
import flash.text.*;

public class GameClass extends MovieClip {

var avatarGuy:avatar;
var fallFruit:FallingObjects;
var points:Number;

public function GameClass(stage)
{
trace("GameClass Working&quot;

points = 0;

var avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

var fallFruit = new FallingObjects();
addChild(fallFruit);

var countdownDisplayMain:CountdownDisplayMain = new CountdownDisplayMain();
countdownDisplayMain.x = 100;
countdownDisplayMain.y = 350;
addChild(countdownDisplayMain);

var scoreBoxInstance:scoreBox = new scoreBox();
scoreBoxInstance.x = 250;
scoreBoxInstance.y = 350;
addChild(scoreBoxInstance);

scoreBoxInstance.score.text = points.toString();

addEventListener(Event.ENTER_FRAME, loop);

function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
var fruit = fallFruit.getChildAt(i);
if(fruit is apple)
{
scoreSystem(10);
}
if(fruit is watermelon)
{
scoreSystem(100);
}
if(fruit is orange)
{
scoreSystem(5)
}
if(fruit is strawberry)
{
scoreSystem(15);
}
fallFruit.removeChild(fallFruit.getChildAt(i));
}
}
}

function scoreSystem(n):void
{
points = points + n;
scoreBoxInstance.score.text = points.toString();
}
}

}
}

weirdlike
offline
weirdlike
1,309 posts
28,720

there are a few ways to achieve this. I prefer adding a function to the class containing the score that returns the value. In your game class add this

public function getFinalScore():Number
{
return points.toString();
}


then add the code in your document class to retrieve the number

public function onGameFinish(event):void
{
game.removeEventListener("GAMEOVER", onGameFinish);

var finalScore:Number = game.getFinalScore();

removeChild(game);
game = null;

trace("finalScore&quot;

}

from there you can just create your text box and show the value

alex57834
offline
alex57834
88 posts
445

C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\Document.as, Line 23 1061: Call to a possibly undefined method getFinalScore through a reference with static type GameClass.

I did what you said but when I try and put the public bit on the getFinalScore function


C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\GameClass.as, Line 76 1114: The public attribute can only be used inside a package.

weirdlike
offline
weirdlike
1,309 posts
28,720

take the getFinalScore and move it outside of the gameclass function

package {

import flash.display.*;
import flash.events.*;
import flash.text.*;

public class GameClass extends MovieClip {

var avatarGuy:avatar;
var fallFruit:FallingObjects;
var points:Number;

public function GameClass(stage)
{
trace("GameClass Working&quot;

points = 0;

var avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

var fallFruit = new FallingObjects();
addChild(fallFruit);

var countdownDisplayMain:CountdownDisplayMain = new CountdownDisplayMain();
countdownDisplayMain.x = 100;
countdownDisplayMain.y = 350;
addChild(countdownDisplayMain);

var scoreBoxInstance:scoreBox = new scoreBox();
scoreBoxInstance.x = 250;
scoreBoxInstance.y = 350;
addChild(scoreBoxInstance);

scoreBoxInstance.score.text = points.toString();

addEventListener(Event.ENTER_FRAME, loop);

function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
var fruit = fallFruit.getChildAt(i);
if(fruit is apple)
{
scoreSystem(10);
}
if(fruit is watermelon)
{
scoreSystem(100);
}
if(fruit is orange)
{
scoreSystem(5)
}
if(fruit is strawberry)
{
scoreSystem(15);
}
fallFruit.removeChild(fallFruit.getChildAt(i));
}
}
}

function scoreSystem(n):void
{
points = points + n;
scoreBoxInstance.score.text = points.toString();
}
}
public function getFinalScore():Number
{
return points.toString();
}

}
}

doing this will fix both the errors. it should look like this

package
{
//imports

public class GameClass extends MovieClip
{
//variable's here

public function GameClass(stage):void
{
//function
}
public function getFinalScore():Number
{
//function
}
}
}

weirdlike
offline
weirdlike
1,309 posts
28,720

clicking auto format button helps to keep the code looking organized

http://i.imgur.com/078gyFY.jpg

alex57834
offline
alex57834
88 posts
445

Thanks for that auto format will be using that ALOT
Yeah I fixed it and got a new error with no clue as how to go about fixing it

C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\GameClass.as, Line 84 1067: Implicit coercion of a value of type String to an unrelated type Number.

weirdlike
offline
weirdlike
1,309 posts
28,720

remove :Number

public function getFinalScore():Number
{
return points.toString();
}

to this

public function getFinalScore():
{
return points.toString();
}

Showing 1-9 of 9