hello there,
I’ve been interested in scripting an OOP game for quite a while, and I’m giving it a try. I’m having some trouble transfering data between classes.
I’ll give an example:
I have a character class which holds all the events for the movement of the character. Let’s say that the character collects an item and should get points for this. All the points are stored in a different class, how do I transfer the points variable from the character class to the points class?
Here’s what I tried, but the points are not added when the hittest is performed:
item/point class
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.DisplayObject;
public class Items extends MovieClip{
public dynamic function Items(target:MovieClip, collectables:MovieClip, collectedNR:Number):void{
addEventListener(Event.ENTER_FRAME, checkHittest);
function checkHittest(e:Event):void{
for(var i:Number = 0; i < collectables.numChildren; i++){
//you know the drill
var item:DisplayObject = collectables.getChildAt(i);
if(target.hitTestObject(item)){
collectedNR += 1;
trace("hit" + i );
remove();
}
}
}//end of check hittest
function remove():void{
//item.visible = false;
}
}//enf of function
}//end of class
}//end of package
character class
package{
import NPC;
import Items;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
public class mechanics extends MovieClip{
//vars
var Key:KeyObject = new KeyObject(stage);
//speed
var xSpeed:Number = 15;
var ySpeed:Number = 20;
//surroundings
var backgroundSpeed = xSpeed/2;
//gravity
var dy:Number = 0;
var gravity:Number = 0.5;
//player
var canjump:Boolean = false;
var grounded:Boolean = false;
//status
var collected:TextField = new TextField();
var collectedNumber:Number = 0;
// -------------------------------------------------------
public function mechanics():void{
//access stage
//npc
var npc:MovieClip = new NPC(this, player, "hallo!
"+"Kan je alle druppels voor me verzamelen :)?");
npc.x = 100;
npc.y = ground.x - ground.height*2 - npc.height;
addChild(npc);
var itamz:Items = new Items(player, itemsHolder, collectedNumber);
addChild(collected);
//event listeners
addEventListener(Event.ENTER_FRAME, onEnter);
//----------------------------------------------------------
function onEnter(e:Event):void{
moveChar();
boundaries();
camera();
statusInterface();
applyGravity();
hittest();
}
function moveChar():void{
if(Key.isDown(Key.LEFT)){
player.x -= xSpeed;
player.scaleX = -0.826690673828125;
player.gotoAndStop("walk");
level.mountains.x -= backgroundSpeed;
scenery.x -= backgroundSpeed/5;
} else if(Key.isDown(Key.RIGHT)){
player.x += xSpeed;
player.scaleX = 0.826690673828125;
player.gotoAndStop("walk");
level.mountains.x += backgroundSpeed;
scenery.x += backgroundSpeed/5;
}else if(Key.isDown(Key.UP) && canjump){
player.gotoAndStop("jump");
dy = -10;
gravity = 0.5;
}else if(Key.isDown(Key.DOWN)){
//duck
}else{
player.gotoAndStop("still");
//nothing
}
}
function boundaries():void{
//left
if(player.x - player.width/2 < ground.x - ground.width/2){
player.x = (ground.x - ground.width/2) + player.width/2;
}
//right
if(player.x + player.width/2 > ground.x + ground.width/2){
player.x = (ground.x + ground.width/2) - player.width/2;
}
}
function camera():void{
cam.x = player.x;
cam.y = player.y;
//left
if(cam.x - cam.width/2 < 0){
backgroundSpeed = 0;
cam.x = 0 + (cam.width/2);
}else
//right
if(cam.x + cam.width/2 >= ground.x + ground.width/2){
cam.x = ground.x + ground.width/2 - cam.width/2;
backgroundSpeed = 0;
}else{
backgroundSpeed = xSpeed/2;
}
//down
if(cam.y + cam.height/2 > stage.stageHeight){
cam.y = stage.stageHeight - cam.height/2;
}
}//end of function
function statusInterface():void{
collected.text = "number of items collected: " + String(collectedNumber);
//collected.autoSize = "LEFT";
collected.x = cam.x - cam.width/2;
collected.y = cam.y - cam.height/2;
}
function applyGravity():void{
dy += gravity;
player.y += dy;
}
function hittest():void{
if(ground.hitTestPoint(player.x, (player.y + player.height/2), true)){
dy = 0;
gravity = 0;
canjump = true;
grounded = true;
}else
if(platforms.hitTestPoint(player.x, (player.y + player.height/2), true)){
dy = 0;
gravity = 0;
canjump = true;
grounded = true;
}else{
canjump = false;
gravity = 0.5;
player.gotoAndStop("jump");
}
}//end of function
//---------------------- END OF ALL -------------------------------
}
}
}
As you can see if this same problem with gravity and the camera. I want to create seperate classes for them, but I can’t connect the gravity and camera to the player when they are in seperate classes.
Help would be appreciated.
Thanks in advance.