-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[haxe][flixel] Add same example of starling to Flixel.
- Loading branch information
Showing
11 changed files
with
349 additions
and
131 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
106 changes: 106 additions & 0 deletions
106
spine-haxe/example/src/flixelExamples/ControlBonesExample.hx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package flixelExamples; | ||
|
||
|
||
import flixel.util.FlxSave; | ||
import flixel.math.FlxPoint; | ||
import flixel.util.FlxColor; | ||
import flixel.util.FlxSpriteUtil; | ||
import flixel.FlxSprite; | ||
import flixel.ui.FlxButton; | ||
import flixel.FlxG; | ||
import spine.flixel.SkeletonSprite; | ||
import spine.flixel.FlixelTextureLoader; | ||
import flixel.FlxState; | ||
import openfl.utils.Assets; | ||
import spine.SkeletonData; | ||
import spine.animation.AnimationStateData; | ||
import spine.atlas.TextureAtlas; | ||
|
||
class ControlBonesExample extends FlxState { | ||
var loadBinary = true; | ||
|
||
private var controlBones = []; | ||
private var controls:Array<FlxSprite> = []; | ||
override public function create():Void { | ||
var button = new FlxButton(0, 0, "Next scene", () -> FlxG.switchState(new EventsExample())); | ||
button.setPosition(FlxG.width * .75, FlxG.height / 10); | ||
add(button); | ||
|
||
var atlas = new TextureAtlas(Assets.getText("assets/stretchyman.atlas"), new FlixelTextureLoader("assets/stretchyman.atlas")); | ||
var data = SkeletonData.from(loadBinary ? Assets.getBytes("assets/stretchyman-pro.skel") : Assets.getText("assets/stretchyman-pro.json"), atlas); | ||
var animationStateData = new AnimationStateData(data); | ||
animationStateData.defaultMix = 0.25; | ||
|
||
var skeletonSprite = new SkeletonSprite(data, animationStateData); | ||
skeletonSprite.scaleX = .5; | ||
skeletonSprite.scaleY = .5; | ||
var animation = skeletonSprite.state.setAnimationByName(0, "idle", true).animation; | ||
skeletonSprite.setBoundingBox(animation); | ||
skeletonSprite.screenCenter(); | ||
add(skeletonSprite); | ||
|
||
var controlBoneNames = [ | ||
"back-arm-ik-target", | ||
"back-leg-ik-target", | ||
"front-arm-ik-target", | ||
"front-leg-ik-target", | ||
]; | ||
|
||
var radius = 6; | ||
for (boneName in controlBoneNames) { | ||
var bone = skeletonSprite.skeleton.findBone(boneName); | ||
var point = [bone.worldX, bone.worldY]; | ||
skeletonSprite.skeletonToHaxeWorldCoordinates(point); | ||
var control = new FlxSprite(); | ||
control.makeGraphic(radius * 2, radius * 2, FlxColor.TRANSPARENT, true); | ||
FlxSpriteUtil.drawCircle(control, radius, radius, radius, 0xffff00ff); | ||
control.setPosition(point[0] - radius, point[1] - radius); | ||
controlBones.push(bone); | ||
controls.push(control); | ||
add(control); | ||
} | ||
|
||
var point = [.0, .0]; | ||
skeletonSprite.beforeUpdateWorldTransforms = function (go) { | ||
for (i in 0...controls.length) { | ||
var bone = controlBones[i]; | ||
var control = controls[i]; | ||
point[0] = control.x + radius; | ||
point[1] = control.y + radius; | ||
go.haxeWorldCoordinatesToBone(point, bone); | ||
bone.x = point[0]; | ||
bone.y = point[1]; | ||
} | ||
}; | ||
|
||
super.create(); | ||
} | ||
|
||
var mousePosition = FlxPoint.get(); | ||
var offsetX:Float = 0; | ||
var offsetY:Float = 0; | ||
var sprite:FlxSprite; | ||
override public function update(elapsed:Float):Void | ||
{ | ||
super.update(elapsed); | ||
|
||
mousePosition = FlxG.mouse.getPosition(); | ||
|
||
for (control in controls) { | ||
if (FlxG.mouse.justPressed && control.overlapsPoint(mousePosition)) | ||
{ | ||
sprite = control; | ||
offsetX = mousePosition.x - sprite.x; | ||
offsetY = mousePosition.y - sprite.y; | ||
} | ||
} | ||
|
||
if (FlxG.mouse.justReleased) sprite = null; | ||
|
||
if (sprite != null) | ||
{ | ||
sprite.x = mousePosition.x - offsetX; | ||
sprite.y = mousePosition.y - offsetY; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package flixelExamples; | ||
|
||
|
||
import flixel.text.FlxText; | ||
import flixel.ui.FlxButton; | ||
import flixel.FlxG; | ||
import flixel.group.FlxSpriteGroup; | ||
import spine.flixel.SkeletonSprite; | ||
import spine.flixel.FlixelTextureLoader; | ||
import flixel.FlxState; | ||
import openfl.utils.Assets; | ||
import spine.SkeletonData; | ||
import spine.animation.AnimationStateData; | ||
import spine.atlas.TextureAtlas; | ||
|
||
class EventsExample extends FlxState { | ||
var loadBinary = true; | ||
|
||
override public function create():Void { | ||
var button = new FlxButton(0, 0, "Next scene", () -> FlxG.switchState(new FlixelState())); | ||
button.setPosition(FlxG.width * .75, FlxG.height / 10); | ||
add(button); | ||
|
||
var atlas = new TextureAtlas(Assets.getText("assets/spineboy.atlas"), new FlixelTextureLoader("assets/spineboy.atlas")); | ||
var data = SkeletonData.from(loadBinary ? Assets.getBytes("assets/spineboy-pro.skel") : Assets.getText("assets/spineboy-pro.json"), atlas, .25); | ||
var animationStateData = new AnimationStateData(data); | ||
animationStateData.defaultMix = 0.25; | ||
|
||
var skeletonSprite = new SkeletonSprite(data, animationStateData); | ||
|
||
// add callback to the AnimationState | ||
skeletonSprite.state.onStart.add(entry -> log('Started animation ${entry.animation.name}')); | ||
skeletonSprite.state.onInterrupt.add(entry -> log('Interrupted animation ${entry.animation.name}')); | ||
skeletonSprite.state.onEnd.add(entry -> log('Ended animation ${entry.animation.name}')); | ||
skeletonSprite.state.onDispose.add(entry -> log('Disposed animation ${entry.animation.name}')); | ||
skeletonSprite.state.onComplete.add(entry -> log('Completed animation ${entry.animation.name}')); | ||
|
||
skeletonSprite.state.setAnimationByName(0, "walk", true); | ||
|
||
var trackEntry = skeletonSprite.state.addAnimationByName(0, "run", true, 3); | ||
skeletonSprite.setBoundingBox(trackEntry.animation); | ||
|
||
skeletonSprite.setBoundingBox(); | ||
skeletonSprite.screenCenter(); | ||
skeletonSprite.skeleton.setBonesToSetupPose(); | ||
add(skeletonSprite); | ||
|
||
trackEntry.onEvent.add( | ||
(entry, event) -> log('Custom event for ${entry.animation.name}: ${event.data.name}')); | ||
|
||
|
||
add(textContainer); | ||
super.create(); | ||
} | ||
|
||
private var textContainer = new FlxSpriteGroup(); | ||
private var logs = new Array<FlxText>(); | ||
private var logsNumber = 0; | ||
private var yOffset = 12; | ||
private function log(text:String) { | ||
var length = logs.length; | ||
var newLog = new FlxText(250, 30, text); | ||
newLog.x = 50; | ||
newLog.y = 20 + yOffset * logsNumber++; | ||
newLog.color = 0xffffffff; | ||
textContainer.add(newLog); | ||
if (logs.length < 35) { | ||
logs.push(newLog); | ||
} else { | ||
logs.shift().destroy(); | ||
logs.push(newLog); | ||
textContainer.y -= yOffset; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.