actionscript 3 - Just one class object works -
i'm trying make collision avoid player move direction hitting side class wall children.
if add more 1 child of wall, added on stage, 1 children gets class commands block (code).
what i'm doing in...
timeline
var canplayermovetotop = true; var canplayermovetobottom = true; var canplayermovetoleft = true; var canplayertoright = true; function addplayertostage(playerx, playery) { var addingplayerto:player = new player(playerx, playery); addchild(addingplayerto); return addingplayerto; } var playeron = addplayertostage(25, 35); function addwall(xchoosen, ychoosen) { var addingwall:testwall = new testwall(); addingwall.x = xchoosen; addingwall.y = ychoosen; addchild(addingwall); return addingwall; } var mynewwall = addwall(155, 55); var anotherwall = addwall(355, 55); wall
package { import flash.display.movieclip; import flash.events.event; public class testwall extends movieclip { public function testwall() { addeventlistener(event.added_to_stage, whenwasthiswalladded); } public function whenwasthiswalladded(e:event):void { addeventlistener(event.enter_frame, checkandupdateground); } public function checkandupdateground(e:event):void { if (this.bottomc.hittestobject(movieclip(root).playeron)) { parent['canplayermovetotop'] = false; } else if (this.topc.hittestobject(movieclip(root).playeron)) { parent['canplayermovetobottom'] = false; } else if (this.leftc.hittestobject(movieclip(root).playeron)) { parent['canplayertoright'] = false; } else if (this.rightc.hittestobject(movieclip(root).playeron)) { parent['canplayermovetoleft'] = false; } else { parent['canplayermovetotop'] = true; parent['canplayermovetobottom'] = true; parent['canplayermovetoleft'] = true; parent['canplayertoright'] = true; } } } }
the cause of problem poor separation of concerns , lack of encapsulation.
separation of concerns
your class wall (testwall bad name class, should start capital name , prepending "test" kind of pointless, because during development, testing) doing (separation of concerns).
it not hittest, sets variables of other object, namely canplayermovetoleft etc. shouldn't that. problem in design.
think way: regular hittestobject returns true or false. doesn't set variables true or false. makes flexible , more universally useful.
you broke not returning value, instead setting variables. it's difference between offering , forcing them take it. doing latter. in order this, wall class has deal variables shouldn't deal with.
encapsulation
using dynamic property access variables bad idea. in case, both objects accessing same variables leads mess, because overwriting results of each other. these 4 variables related player (as said, classes should start capital letters). that's why should part of class , not floating around individual variables.
solution player class
add 4 variables define if player can move in direction player class. below example code shows 1 such variable. note how makes unnecessary include player in name of variable
package { import flash.display.sprite; private movabletop:boolean = true; public class player extends sprite { public function set canmovetotop(value:boolean):void { movabletop = value; } } } no, not recommended bloat constructor parameters x , y. if want set properties, did wall class: set properties separately.
furthermore, add method reset convenience sets 4 variables true;
solution wall class
the wall class should provide functionality hittesting against player. again, @ how built in as3 api works, hittestobject doesn't run every frame on own arbitrary object. instead, have explicitly call function , pass object parameter. simplified wall this:
package { import flash.display.sprite; public class wall extends sprite { public function hittestplayer(player:player):void { if(bottomc.hittestobject(player)) { player.canmovetotop = false; } else if(topc.hittestobject(player)) { player.canmovetobottom = false; } else if(leftc.hittestobject(player) { player.canmovetoright = false; } else if(rightc.hittestobject(player) { player.canmovetoleft = false; } } } } because player object passed function valid change variables in hittest function. alternative, return value represents result of hittesting
solution main class formerly known timeline
don't put code on timeline. use document class, call main. logic of repeatedly calling hittests , other functions should located.
package { import flash.display.sprite; public class main extends sprite { private var player:player; private var walls:array = []; public function main():void { player = addplayerat(25, 35); addwallat(155, 55); addwallat(355, 55); addeventlistener(event.enter_frame, loop); } public function loop(e:event):void { player.reset(); each(var wall:wall in walls) { wall.hittestplayer(player); } } private function addplayerat(x:number, y:number):player { var player:player = new player(); player.x = x; player.y = y; addchild(player); return player; } private function addwallat(x:number, y:number):wall { var wall:wall = new wall(); wall.x = x; wall.y = y; addchild(wall); walls.push(wall); return wall; } } } code untested, idea.
Comments
Post a Comment