Skip to content

Commit

Permalink
[as3][starling] Extended SpineboyExample to show how to use bounding …
Browse files Browse the repository at this point in the history
…box world vertices.
  • Loading branch information
badlogic committed Mar 14, 2018
1 parent 9e3c318 commit e20428b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
76 changes: 76 additions & 0 deletions spine-starling/spine-starling-example/src/spine/examples/Shape.as
@@ -0,0 +1,76 @@
package spine.examples {
import starling.animation.IAnimatable;
import starling.textures.Texture;
import flash.display.BitmapData;
import flash.geom.Point;
import spine.starling.SkeletonMesh;

import starling.display.DisplayObject;
import starling.rendering.IndexData;
import starling.rendering.Painter;
import starling.utils.Color;


public class Shape extends DisplayObject implements IAnimatable {
private var r: Number = 1, g: Number = 1, b: Number = 1, a: Number = 1;
private var mesh: SkeletonMesh;
private var vertices: Vector.<Number>;

public function Shape() {
var bitmapData: BitmapData = new BitmapData(16, 16, false, 0xffffffff);
mesh = new SkeletonMesh(Texture.fromBitmapData(bitmapData));
setVertices(new <Number>[0, 0, 100, 0, 100, 100, 0, 100]);
setColor(1, 0, 0, 1);
}

public function setVertices(vertices: Vector.<Number>): void {
this.vertices = vertices;
}

public function setColor(r: Number, g: Number, b: Number, a: Number): void {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}

override public function render(painter : Painter) : void {
var indices: IndexData = mesh.getIndexData();
var idx: int = 0;
var x:Number = vertices[0], y:Number = vertices[1];
for (var i:int = 2; i < vertices.length - 2; i+=2) {
var x2:Number = vertices[i], y2:Number = vertices[i+1];
var x3:Number = vertices[i+2], y3:Number = vertices[i+3];
indices.setIndex(idx, idx);
indices.setIndex(idx+1, idx+1);
indices.setIndex(idx+2, idx+2);
mesh.setVertexPosition(idx, x, y);
mesh.setTexCoords(idx++, 0, 0);
mesh.setVertexPosition(idx, x2, y2);
mesh.setTexCoords(idx++, 0, 0);
mesh.setVertexPosition(idx, x3, y3);
mesh.setTexCoords(idx++, 0, 0);
}
indices.numIndices = idx;
indices.trim();
mesh.getVertexData().numVertices = idx;

var rgb: uint = Color.rgb(r * 255, g * 255, b * 255);
var alpha: uint = a * 255;
mesh.getVertexData().colorize("color", 0xffffffff, 0xff);

mesh.setVertexDataChanged();
mesh.setIndexDataChanged();

painter.batchMesh(mesh);
}

public function advanceTime(time : Number) : void {
this.setRequiresRedraw();
}

override public function hitTest(localPoint : Point) : DisplayObject {
return null;
}
}
}
Expand Up @@ -29,6 +29,10 @@
*****************************************************************************/

package spine.examples {
import starling.display.Image;
import starling.textures.Texture;
import flash.display.BitmapData;
import spine.attachments.BoundingBoxAttachment;
import spine.*;
import spine.animation.AnimationStateData;
import spine.animation.TrackEntry;
Expand All @@ -54,7 +58,8 @@ package spine.examples {

[Embed(source = "/spineboy.png")]
static public const SpineboyAtlasTexture : Class;
private var skeleton : SkeletonAnimation;
private var skeleton : SkeletonAnimation;
private var shape: Shape;

public function SpineboyExample() {
var spineAtlas : Atlas = new Atlas(new SpineboyAtlas(), new StarlingTextureLoader(new SpineboyAtlasTexture()));
Expand All @@ -71,6 +76,7 @@ package spine.examples {
skeleton = new SkeletonAnimation(skeletonData, stateData);
skeleton.x = 400;
skeleton.y = 560;
skeleton.scale = 0.5;

skeleton.state.onStart.add(function(entry : TrackEntry) : void {
trace(entry.trackIndex + " start: " + entry.animation.name);
Expand All @@ -97,10 +103,29 @@ package spine.examples {
skeleton.state.addAnimationByName(0, "run", true, 0);

addChild(skeleton);
Starling.juggler.add(skeleton);
Starling.juggler.add(skeleton);

shape = new Shape();
shape.setVertices(new <Number>[0, 0, 400, 600, 800, 0]);
shape.setColor(1, 0, 0, 1);
addChild(shape);
Starling.juggler.add(shape);

addEventListener(starling.events.Event.ENTER_FRAME, onUpdate);
addEventListener(TouchEvent.TOUCH, onClick);
}

private function onUpdate() : void {
var slot:Slot = skeleton.skeleton.findSlot("head-bb");
var bb:BoundingBoxAttachment = skeleton.skeleton.getAttachmentForSlotIndex(slot.data.index, "head") as BoundingBoxAttachment;
var worldVertices:Vector.<Number> = new Vector.<Number>(bb.worldVerticesLength);
bb.computeWorldVertices(slot, 0, bb.worldVerticesLength, worldVertices, 0, 2);
for (var i:int = 0; i < worldVertices.length; i+=2) {
worldVertices[i] = worldVertices[i] * skeleton.scale + skeleton.x;
worldVertices[i + 1] = worldVertices[i + 1] * skeleton.scale + skeleton.y;
}
shape.setVertices(worldVertices);
}

private function onClick(event : TouchEvent) : void {
var touch : Touch = event.getTouch(this);
Expand Down

0 comments on commit e20428b

Please sign in to comment.