Created
December 8, 2010 21:54
-
-
Save zachwlewis/733985 to your computer and use it in GitHub Desktop.
Splitscreen Game World Rendering
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 flash.display.BitmapData; | |
import flash.events.FocusEvent; | |
import flash.geom.Point; | |
import flash.geom.Rectangle; | |
import net.flashpunk.Entity; | |
import net.flashpunk.FP; | |
import net.flashpunk.graphics.Backdrop; | |
import net.flashpunk.graphics.Image; | |
import net.flashpunk.utils.Key; | |
import net.flashpunk.World; | |
/** | |
* ... | |
* @author Zachary Weston Lewis | |
*/ | |
public class GameWorld extends World | |
{ | |
[Embed(source = 'bg.jpg')] protected const BG:Class; | |
protected var p1viewport:BitmapData; | |
protected var p2viewport:BitmapData; | |
protected var p1:Player; | |
protected var p2:Player; | |
public function GameWorld() | |
{ | |
p1viewport = new BitmapData(200, 200); | |
p2viewport = new BitmapData(200, 200); | |
} | |
override public function begin():void | |
{ | |
p1 = Player(add(new Player(Key.W, Key.S, Key.A, Key.D, 0xff0000))); | |
p2 = Player(add(new Player(Key.UP, Key.DOWN, Key.LEFT, Key.RIGHT, 0x00ff00))); | |
var e:Entity = addGraphic(new Backdrop(BG), 10); | |
super.begin(); | |
} | |
override public function render():void | |
{ | |
// I'm just going to hack this to get two screens. | |
// First, let's render the screen at player 1. | |
FP.camera = new Point(p1.x - 92, p1.y - 92); | |
super.render(); | |
// Now, we'll copy this to our p1viewport. | |
p1viewport.copyPixels(FP.buffer, new Rectangle(0, 0, 200, 200), new Point); | |
// Repeat with the screen at player 2. | |
FP.screen.refresh(); | |
FP.camera = new Point(p2.x - 92, p2.y - 92); | |
super.render(); | |
p2viewport.copyPixels(FP.buffer, new Rectangle(0, 0, 200, 200), new Point); | |
// Now, we'll draw this back onto our buffer. | |
FP.buffer.copyPixels(p1viewport, new Rectangle(0, 0, 200, 200), new Point); | |
FP.buffer.copyPixels(p2viewport, new Rectangle(0, 0, 200, 200), new Point(200, 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 flash.display.BitmapData; | |
import flash.geom.Point; | |
import net.flashpunk.Entity; | |
import net.flashpunk.FP; | |
import net.flashpunk.graphics.Image; | |
import net.flashpunk.utils.Input; | |
import net.flashpunk.utils.Key; | |
/** | |
* ... | |
* @author Zachary Weston Lewis | |
*/ | |
public class Player extends Entity | |
{ | |
protected var key_up:int; | |
protected var key_down:int; | |
protected var key_left:int; | |
protected var key_right:int; | |
public function Player(up:int, down:int, left:int, right:int, color:uint) | |
{ | |
// Set up player keys. | |
key_up = up; | |
key_down = down; | |
key_left = left; | |
key_right = right; | |
// Set up player color. | |
graphic = new Image(new BitmapData(16, 16)); | |
Image(graphic).color = color; | |
} | |
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 * FP.elapsed; | |
y += m.y * 50 * FP.elapsed; | |
super.update(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment