How to do effective split screening?


(John Andersson) #1

Hi, I’m trying to implement a horizontal split screen. I googled and found this https://gist.github.com/zachwlewis/733985 but it doesn’t explain how to make the screens follow the two players I’m also confused at how to remake it into horizontal…

So how do I make two different cameras follow two different players with split screen? Thanks


(Martí Angelats i Ribera) #2

You basically pre-render every split, save them in 2 BitmapData and then making the final render that two splits together.

PS: The 200 should be changed depending of your game sizes.


(John Andersson) #3

I got that from the git :stuck_out_tongue: But I don’t understand how to track the two players with two cameras at once


(Martí Angelats i Ribera) #4

In the code you licked it doesn’t use to cameras. I think you want something like this:

public class splitedScreen extends World //actually extends the world you want
{
public var camera1:Point;
public var camera2:Point;

private var _view1:BitmapData;
private var _view2:BitmapData;

private var _rect1:Rectangle;
private var _rect2:Rectangle;

private var _copyOffset:Point;


public function splitedScreen()
{
	super();
	//we create a second camera (we can't use camera)
	camera1 = new Point;
	camera2 = new Point;
	
	//we set where will go the two part of the screens (save that in two rectangles)
	//Changing the values here will modify the size of the splited screens. This is vertical but can be in any rectangular form.
	_rect1 = new Rectangle(0,      0,      FP.width, FP.height/2);
	_rect2 = new Rectangle(0, FP.height/2, FP.width, FP.height/2);
	
	//we create the bitmapDatas with its size
	_view1 = new BitmapData(_rect1.width, _rect1.height);
	_view2 = new BitmapData(_rect2.width, _rect2.height);
	
	//You may not want to get the buffer in the 0,0 so you can take an offset
	//This offset is equal in both views but you can set twoo different views.
	_copyOffset = new Point;
}

override public function render():void
{
	//here goes the magic part. FP renders everything in a BitmapData. So the idea is to render, copy that buffer, change the camera, render again, save that buffer too and then getting both of them.
	
	camera = camera1;
	super.render();
	_view1.copyPixels(FP.buffer, _rect1, _rect1.topLeft);
	
	FP.screen.refresh();
	
	camera = camera2;
	super.render();
	_view2.copyPixels(FP.buffer, _rect2, _rect2.topLeft);
	
	FP.screen.refresh();
	
	//finally we render both screens. If overlaps the _view1 have priority.
	FP.buffer.copyPixels(_view2, _rect2, _rect2.topLeft);
	FP.buffer.copyPixels(_view1, _rect1, _rect1.topLeft);
	
}
}

(John Andersson) #5

Thank you, I will try this. So I make an as like that which extends the game world? How do I add the splitedscreen class to the world? In the world itself like “fp.world.add(new Splitedscreen)”?