nativeScript-physics-js
by vakrilov | v2.0.1
PhysicsJS 库的 NativeScript 渲染器。
npm i --save nativescript-physics-js

NativeScript 的 PhysicsJS

这是 PhysicsJS 库的 NativeScript 渲染器。

安装

tns plugin add nativescript-physics-js

使用方法

在 XML 标记(main-page.xml)中

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
<!-- Definte the container for the phisics scene -->
<GridLayout id="container" width="300" height="300" backgroundColor="lightgreen">

<!-- Label for meta info is note requiered -->
<Label id="meta"/>
</GridLayout>
</Page>

在 JavaScript(main-page.js)中

var Physics = require("nativescript-physics-js");

var init = false;
function pageLoaded(args) {
// Prevent double initialization
if (init) {
return;
}

// Get references to container and meta-info views
var page = args.object;
var container = page.getViewById("container");
var metaText = page.getViewById("meta");

// Create physics world and configure NS renderer
var world = Physics();
world.add(Physics.renderer('ns', {
container: container,
metaText: metaText,
meta: true
}));

// Add bodies
world.add(Physics.body('circle', {
x: 80,
y: 50,
radius: 15,
styles: { image: "~/images/ns-logo.png" }
}));

// Add behaviors
world.add([
Physics.behavior('edge-collision-detection', { aabb: Physics.aabb(0, 0, 300, 300) }),
Physics.behavior('body-collision-detection'),
Physics.behavior('body-impulse-response'),
Physics.behavior('sweep-prune'),
Physics.behavior('constant-acceleration') // Gravity
]);

// Start ticking...
world.on('step', function () { world.render() });
setInterval(function () { world.step(Date.now()); }, 20);
}
exports.pageLoaded = pageLoaded;

示例

演示代码 在此处

更多演示在 PhysicsJS 网站上