//......imports...... public class Main extends Sprite { private var m_model:Model; private var m_view:View; private var m_controller:Controller; public function Main() { m_model = new Model(); m_controller = new Controller(m_model); m_view = new View(m_model, m_controller); addChild(m_view); } }
//......imports...... public class Main extends Sprite { private var m_model:Model; private var m_view:View; private var m_controller:Controller; public function Main() { m_model = new Model(); m_controller = new Controller(m_model); m_view = new View(m_model, m_controller); addChild(m_view); } }
//......imports...... public class View extends Sprite { [Embed(source="my_small_spaceship_image.jpg")] public var m_image:Class; public var m_spaceShip:Bitmap = new m_image(); private var m_model:Object; private var m_controller:Object; public function View(model:Object, controller:Object):void { m_model = model; m_model.addEventListener(Event.CHANGE, changed); m_controller = controller; m_spaceShip.x = 200; m_spaceShip.y = 200; addChild(m_spaceShip); addEventListener(Event.ADDED_TO_STAGE, addedToStage); } public function addedToStageHandler(event:Event):void { stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); } public function keyPressed(event:KeyboardEvent):void { m_controller.key_Pressed(event); } public function changed(event:Event):void { m_spaceShip.x = m_model.direction[0] m_spaceShip.y = m_model.direction[1]; } }
//......imports...... public class View extends Sprite { [Embed(source="my_small_spaceship_image.jpg")] public var m_image:Class; public var m_spaceShip:Bitmap = new m_image(); private var m_model:Object; private var m_controller:Object; public function View(model:Object, controller:Object):void { m_model = model; m_model.addEventListener(Event.CHANGE, changed); m_controller = controller; m_spaceShip.x = 200; m_spaceShip.y = 200; addChild(m_spaceShip); addEventListener(Event.ADDED_TO_STAGE, addedToStage); } public function addedToStageHandler(event:Event):void { stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); } public function keyPressed(event:KeyboardEvent):void { m_controller.key_Pressed(event); } public function changed(event:Event):void { m_spaceShip.x = m_model.direction[0] m_spaceShip.y = m_model.direction[1]; } }
m_spaceShip.x = m_model.direction[0]
and calls the get method. public class Controller public class Controller { private var m_model:Object; public function Controller(model:Object):void { m_model = model; } public function key_Pressed(event:KeyboardEvent):void { switch (event.keyCode) { case Keyboard.LEFT: m_model.direction = new Array(m_model.direction[0] - 5, m_model.direction[1]); break; case Keyboard.RIGHT: m_model.direction = new Array(m_model.direction[0] + 5, m_model.direction[1]); break; case Keyboard.UP: m_model.direction = new Array(m_model.direction[0], m_model.direction[1] - 5); break; case Keyboard.DOWN: m_model.direction = new Array(m_model.direction[0], m_model.direction[1] + 5); break; } } }
public class Controller public class Controller { private var m_model:Object; public function Controller(model:Object):void { m_model = model; } public function key_Pressed(event:KeyboardEvent):void { switch (event.keyCode) { case Keyboard.LEFT: m_model.direction = new Array(m_model.direction[0] - 5, m_model.direction[1]); break; case Keyboard.RIGHT: m_model.direction = new Array(m_model.direction[0] + 5, m_model.direction[1]); break; case Keyboard.UP: m_model.direction = new Array(m_model.direction[0], m_model.direction[1] - 5); break; case Keyboard.DOWN: m_model.direction = new Array(m_model.direction[0], m_model.direction[1] + 5); break; } } }
public class Model extends EventDispatcher { private var m_direction:Array = new Array(); public function Model() { m_direction.push(200, 200); } public function get direction():Array { return m_direction; } public function set direction(m_values:Array):void { m_direction[0] = m_values[0]; m_direction[1] = m_values[1]; dispatchEvent(new Event(Event.CHANGE)); } }
public class Model extends EventDispatcher { private var m_direction:Array = new Array(); public function Model() { m_direction.push(200, 200); } public function get direction():Array { return m_direction; } public function set direction(m_values:Array):void { m_direction[0] = m_values[0]; m_direction[1] = m_values[1]; dispatchEvent(new Event(Event.CHANGE)); } }
Source: https://habr.com/ru/post/130189/
All Articles