2 dimensions of awesome: advanced actionscript for platform games by iain lobb

112
Two Dimensions of Awesome By Iain Lobb

Upload: mochimedia

Post on 21-Jan-2015

4.988 views

Category:

Technology


1 download

DESCRIPTION

Iain Lobb provides some tips and tricks on how to optimize your 2D Flash games!

TRANSCRIPT

Page 1: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Two Dimensions of AwesomeBy Iain Lobb

Page 2: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Two Dimensions of

Awesome.

Page 3: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

I don’t have the

awesome.*

Page 4: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

I’m looking for the

awesome.

Page 5: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

*I’ve found some of it.

Page 6: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

First, let’s troll…

Page 7: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

The typical Flash game…

• Stickmen!• Arenas!• 20fps!• 550 x 400!

Page 8: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
Page 9: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

The retro Flash game…• 8-bit pixel art!• 2 frame animations!• Tile grid!• Everything is a square!• Double pixels! You want to see those

pixels, right?• 3rd party engine!

Page 10: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
Page 11: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Now let’s see what they’re doing on games

consoles…

Page 12: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Ray Man Origins

Page 13: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Limbo

Page 14: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

The 2D awesome…• HD graphics.• 60 frames per second• Smooth scrolling camera. • Multiple layers of parallax.• Uneven, organic terrain.• Expressive character animation.

Page 15: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

BunnyMark• 26x37 pixel sprite• 30fps• 640x480

Page 16: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #1BunnyMark - Bitmaps

Page 17: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

4000 bunnies

Page 18: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #2BunnyMark - Blitting

Page 19: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

6000 bunnies

Page 20: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #3BunnyMark – HTML5 Canvas

Page 21: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

2000 bunnies

Page 22: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Bunny Domination!

Page 23: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

BunnyLandMark• A more realistic scenario• 30fps• 640x480• 26x37 pixel sprite• Scrolling world• Depth-sorted sprites

Page 24: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #4BunnyLandMark – Bitmaps

Page 25: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

22,000 bunnies

Page 26: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #5BunnyLandMark – Blitting

Page 27: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

90,000 bunnies!

Page 28: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Now something shocking…

Page 29: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Soylent Green is people!

Page 30: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

BunnyMark is a lie.

Page 31: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

60 fps

Page 32: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #6BunnyMark – Bitmaps (60fps)

Page 33: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

1500 bunnies

Page 34: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #7BunnyMark – Blitting (60fps)

Page 35: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

1500 bunnies

Page 36: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

HD resolution• Typically Flash game = 640x480• Portal maximum = 800x600• Nitrome = 500x500 pixels• Rayman Origins = 1080p =

1920x1080• 500x500 can fit into 1080p eight

times!

Page 37: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

960x720

Page 38: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #8BunnyMark –

Bitmaps (60fps)960 x 720

Page 39: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

1000 bunnies

Page 40: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #9BunnyMark –

Blitting (60fps)960 x 720

Page 41: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

1000 bunnies

Page 42: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Sprites• 256x148•Digital painting• Scale +/- 50%• Rotation

Page 43: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Introducing PirateMark!

• 60 fps• 960x720• 256x148 sprite• Scaling +/- 50%• Rotation• How many sprites can we have?

Page 44: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #10PirateMark –

Bitmaps (60fps)

Page 45: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Page 46: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

20 pirates

Page 47: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #11PirateMark –

Bitmaps (30fps)

Page 48: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

40 pirates

Page 49: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #12PirateMark –

Blitting (60fps)

Page 50: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

15 pirates

Page 51: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #13PirateMark –

Blitting (30fps)

Page 52: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

80 pirates

Page 53: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Stage3D

Page 54: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #14PirateMark –

ND2D

Page 55: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

20 pirates

Page 56: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #15PirateMark – Genome2D

Page 57: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

80 pirates

Page 58: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #16PirateMark –

Starling

Page 59: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

100 pirates

Page 60: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #17PirateMark – haXe NME

Page 61: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

150 pirates

Page 62: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #18PirateMark –

HTML5 Canvas

Page 63: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

0 pirates

Page 64: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

100 pirates

Page 65: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

He who controls the graphics card

drivers, controls the universe!

Page 66: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Can we support Stage3D and

software rendering?

Page 67: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Can we support 30fps and 60fps?

Page 68: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

// Frame-based Euler

function update(){ speedY += gravity; speedX *= drag;

speedY *= drag; x += speedX; y += speedY;}

Page 69: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

// Time-based Improved Euler

function update(time:Number){ speedY += gravity * time; speedX *= Math.pow(drag, time); speedY *= Math.pow(drag, time); x += (speedX + oldSpeedX) / 2; y += (speedY + oldSpeedY) / 2; oldSpeedX = speedX; oldSpeedY = speedY;}

Page 70: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #19Improved

Euler

Page 71: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

...or fix your time step and interpolate.

Page 72: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Camera and Parallax Scrolling

Page 73: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

• A 2D camera is just an X and Y offset.• Subtract the camera position from

each sprite’s world position to get its screen position.

2D Camera

Page 74: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

// Simple 2D Camera

for each (var sprite in sprites){ sprite.x = sprite.worldX – cameraX; sprite.y = sprite.worldY – cameraY;}

Page 75: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

• By using a container sprite we can make this process even simpler.

• Set the container position to negative the camera position.

• Each sprite will automatically get the correct screen position.

2D Camera

Page 76: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

// 2D Camera using container

world.addChild(sprite);

function update(){ world.x = –cameraX; world.y = –cameraY;}

Page 77: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

• To enable zooming, we add a second container.

• We move the inner container and scale the outer container.

Zooming

Page 78: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

// Zooming 2D Camera

world.addChild(sprite);container.addChild(world);

container.x = stage.stageWidth / 2;container.y = stage.stageHeight / 2;

function update(){ world.x = –cameraX; world.y = –cameraY; container.scaleX = container.scaleY = zoom;}

Page 79: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #21Zooming Camera

Page 80: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

• To enable parallax, we position the inner container to negative the camera position, multiplied by a fraction.

Parallax

Page 81: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

// Zooming 2D Camera with parallax

world.addChild(sprite);container.addChild(world);

container.x = stage.stageWidth / 2;container.y = stage.stageHeight / 2;

function update(){ world.x = –cameraX * 0.5; world.y = –cameraY * 0.5; container.scaleX = container.scaleY = zoom;}

Page 82: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #22Parallax Camera

Page 83: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #23Starling Camera

Page 84: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Uneven Terrain

Page 85: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

hitTestPoint(x, y, shapeFlag);

Page 86: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #24hitTestPoint

Page 87: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

hitTestPoint• Only works +/- 2000 pixels• Lags by one frame if you move the container.• Must be on the stage but doesn’t have to be

visible.• Reduce number of calls using AABB broad

phase.• Same principles can be applied using

getPixel32

Page 88: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Bullet

Page 89: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Arrow

Page 90: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Fast moving bullet

Page 91: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Character

Page 92: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Character

Page 93: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Grenade

Page 94: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Grenade

Page 95: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Grenade

Page 96: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Grenade

Page 97: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #25Projectiles

Page 98: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Collision Detection• Box2D / Nape• Straight lines• Why hitTestPoint?

Page 99: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Level Editor• Position objects• Rotate and scale objects• Draw collision areas• Give instance names for code access• Save, Save As, Undo, Redo, Copy, Paste• Zoom

Page 100: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

It’s Flash Professional!

Page 101: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

It’s Flash Professional!

Page 102: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

It’s Flash Professional!

Page 103: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

It’s Flash Professional!

Page 104: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Parsing with ActionScript

• Create instance of level• Loop through children.• Determine type of child using

“is”• If no linkage found, assume

terrain

Page 105: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Gotchas

• Small scroll area – scale down and zoom in

• “Invalid size and position”• Flash CS5 crashes creating large SWCs• Expensive

Page 106: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Character Animation

Page 107: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Timelines leak memory!

Page 108: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Sprite Sheets

• Flash, After Effects, Blender• Great performance• High memory use• Not changeable at runtime

Page 109: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Bones• Not the Bone Tool!• Separate sprites for each body part• Saves memory• Adjustable playback speed• Customizable• Parse timeline – animations are pure

data

Page 110: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #26Bones

Page 111: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Demo #28Alice Gameplay

Page 112: 2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb

Final thoughts…