Actionscript: Passing public variable from one class to another? -
i'm stuck on creating hit detection function inside object class file if collides player on stage detects it. i'm getting "error:1067 access of undefined property" public variables call document class file. here function inside object class want call public variables for:
public function hittesting(obj:object) { if (hittestpoint(obj.x + leftbumppoint.x, obj.y + pubvarsleftbumppoint.y, true)) { trace("leftcollide"); leftcollide = true; } else { leftcollide = false; } if (hittestpoint(obj.x + rightbumppoint.x, obj.y + rightbumppoint.y, true)) { trace("right hit"); rightcollide = true; } else { rightcollide = false; } if (hittestpoint(obj.x + upbumppoint.x, obj.y + upbumppoint.y, true)) { trace("up hit"); upcollide = true; } else { upcollide = false; } }
edit: code document class:
package { import flash.events.event; import flash.events.keyboardevent; import flash.events.*; import flash.ui.keyboard; import flash.display.*; import flash.utils.timer; import flash.sampler.newobjectsample; import flash.geom.point; import mediumenemy; public class publicvariables extends movieclip { public var uppressed: boolean = false; public var downpressed: boolean = false; public var leftpressed: boolean = false; public var rightpressed: boolean = false; public var xspeed: number = 0; public var yspeed: number = 0; public var gravity: number = 1; public var scrollx: number = 0; public var scrolly: number = 0; public var speedconstant: int = 8; public var friction: number = 0.55; public var leftcollide: boolean = false; public var rightcollide: boolean = false; public var upcollide: boolean = false; public var downcollide: boolean = false; public var leftbumppoint: point = new point(-30, -55); public var rightbumppoint: point = new point(30, -55); public var upbumppoint: point = new point(0, -120); public var downbumppoint: point = new point(0, 80); public var jumpconstant: number = -65; public var gravityconstant: number = 5; public function publicvariables() {
code object class:
package { import flash.display.movieclip; import flash.events.event; import flash.events.timerevent; import flash.utils.timer; import flash.geom.point; import publicvariables; public class mediumenemy extends publicvariables { //private var xspeed: number = 8 //randomvalue(3, 6); //not being used //private var yspeed: number = 8 //randomvalue(8, 15); not being used private var direction: boolean = false; public function mediumenemy(xlocation: number, ylocation: number) { x = xlocation + randomvalue(0, 550); y = ylocation; //set x position random value if (x < 275) //determine whether make direction of motion left or right { direction = true //trace("go left") } else { direction = false; //trace("go right"); } this.addeventlistener(event.enter_frame, animate); //add } function animate(event: event) { //looping code switch (direction) { case false: { x -= xspeed; break; } case true: //go right { x += xspeed; break; } } y += yspeed; } public function hittesting(obj: object) { if (hittestpoint(obj.x + leftbumppoint.x, obj.y + leftbumppoint.y, true)) { trace("leftcollide"); leftcollide = true; } else { leftcollide = false; } if (hittestpoint(obj.x + rightbumppoint.x, obj.y + rightbumppoint.y, true)) { trace("right hit"); rightcollide = true; } else { rightcollide = false; } if (hittestpoint(obj.x + upbumppoint.x, obj.y + upbumppoint.y, true)) { trace("up hit"); upcollide = true; } else { upcollide = false; } } } }
fyi: intending on somehow passing 'player' object hittesting function x position using parameter.
to suggest approach necessary analyse code , understand cause issues, etc.
try this:
public class mediumenemy extends movieclip { private var direction:boolean = false; private var publicvariables:publicvariables = new publicvariables(); public function mediumenemy(xlocation:number, ylocation:number) { x = xlocation + randomvalue(0, 550); y = ylocation; // set x position random value if (x < 275) // determine whether make direction of motion left or right { direction = true; // trace("go left") } else { direction = false; // trace("go right"); } this.addeventlistener(event.enter_frame, animate); // add } private function animate(event:event) { // looping code switch (direction) { case false: { x -= publicvariables.xspeed; break; } case true: // go right { x += publicvariables.xspeed; break; } } y += publicvariables.yspeed; } public function hittesting(obj:object) { if (hittestpoint(obj.x + publicvariables.leftbumppoint.x, obj.y + publicvariables.leftbumppoint.y, true)) { trace("leftcollide"); publicvariables.leftcollide = true; } else { publicvariables.leftcollide = false; } if (hittestpoint(obj.x + publicvariables.rightbumppoint.x, obj.y + publicvariables.rightbumppoint.y, true)) { trace("right hit"); publicvariables.rightcollide = true; } else { publicvariables.rightcollide = false; } if (hittestpoint(obj.x + publicvariables.upbumppoint.x, obj.y + publicvariables.upbumppoint.y, true)) { trace("up hit"); publicvariables.upcollide = true; } else { publicvariables.upcollide = false; } } }
Comments
Post a Comment