Created
May 22, 2013 08:44
-
-
Save julsam/5626155 to your computer and use it in GitHub Desktop.
HaxePunk Splitscreen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import com.haxepunk.HXP; | |
import com.haxepunk.Scene; | |
import com.haxepunk.utils.Key; | |
import nme.display.BitmapData; | |
import nme.geom.Point; | |
import nme.geom.Rectangle; | |
class GameScene extends Scene | |
{ | |
private var p1viewport:BitmapData; | |
private var p2viewport:BitmapData; | |
private var p1:Player; | |
private var p2:Player; | |
public function new() | |
{ | |
super(); | |
p1viewport = new BitmapData(200, 200); | |
p2viewport = new BitmapData(200, 200); | |
} | |
override public function begin():Void | |
{ | |
p1 = add(new Player(Key.W, Key.S, Key.A, Key.D, 0xff0000)); | |
p2 = add(new Player(Key.UP, Key.DOWN, Key.LEFT, Key.RIGHT, 0x00ff00)); | |
} | |
public override function render() | |
{ | |
HXP.camera = new Point(p1.x - 160, p1.y - 240); | |
super.render(); | |
p1viewport.copyPixels(HXP.buffer, new Rectangle(0, 0, 320, 480), new Point(0, 0)); | |
HXP.screen.refresh(); | |
HXP.camera = new Point(p2.x - 160, p2.y - 240); | |
super.render(); | |
p2viewport.copyPixels(HXP.buffer, new Rectangle(0, 0, 320, 480), new Point(0, 0)); | |
HXP.buffer.copyPixels(p1viewport, new Rectangle(0, 0, 320, 480), new Point(0, 0)); | |
HXP.buffer.copyPixels(p2viewport, new Rectangle(0, 0, 320, 480), new Point(320, 0)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import com.haxepunk.HXP; | |
import com.haxepunk.Entity; | |
import com.haxepunk.graphics.Image; | |
import com.haxepunk.utils.Input; | |
import nme.display.BitmapData; | |
import nme.geom.Point; | |
class Player extends Entity | |
{ | |
private var key_up:Int; | |
private var key_down:Int; | |
private var key_left:Int; | |
private var key_right:Int; | |
public function new(up:Int, down:Int, left:Int, right:Int, color:Int=0xffffff) | |
{ | |
super(); | |
graphic = new Image(new BitmapData(16, 16, false, color)); | |
// Set up player keys. | |
key_up = up; | |
key_down = down; | |
key_left = left; | |
key_right = right; | |
} | |
override public function update():Void | |
{ | |
var m:Point = new Point(); | |
if (Input.check(key_up)) m.y--; | |
if (Input.check(key_down)) m.y++; | |
if (Input.check(key_left)) m.x--; | |
if (Input.check(key_right)) m.x++; | |
x += m.x * 50 * HXP.elapsed; | |
y += m.y * 50 * HXP.elapsed; | |
super.update(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment