vakrilov / nativescript-physics-js

MIT License
9 stars 4 forks source link

Unable to load sprites images and create a movement like run or jump #4

Open memePushpendu opened 7 years ago

memePushpendu commented 7 years ago

Can i use pixi renderer in here?

memePushpendu commented 7 years ago

MY CODE

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

var ninja;
/** basic image sources **/
var runSprites = [
    '~/images/sprites/ninja/Run__000.png',
    '~/images/sprites/ninja/Run__001.png',
    '~/images/sprites/ninja/Run__002.png',
    '~/images/sprites/ninja/Run__003.png',
    '~/images/sprites/ninja/Run__004.png',
    '~/images/sprites/ninja/Run__005.png',
    '~/images/sprites/ninja/Run__006.png',
    '~/images/sprites/ninja/Run__007.png',
    '~/images/sprites/ninja/Run__008.png',
    '~/images/sprites/ninja/Run__009.png'
];

var init = false;

var count;

var container;
var metaText;

var world;

function doFullscreen() {
    require('application').android.startActivity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

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

    count = 0;

    doFullscreen();

    var screenHeight = platform.screen.mainScreen.heightPixels;
    var screenWidth = platform.screen.mainScreen.widthPixels;

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

    container.height = 360;
    container.width = 640;

    world = Physics();
    var renderer = Physics.renderer('ns', {
        container: container,
        metaText: metaText,
        meta: true
    });

    world.add(renderer);

    // Add bodies

    // Start ticking...
    world.on('step', function () {
        console.log(count);

        addNinja(runSprites[count])
            .then(function (success) {
                world.render();
                count++;
                if (count == 10) {
                    count = 0;
                }
            });
    });

    setInterval(function () { world.step(Date.now()); }, 1000);  // refreshing world
}
exports.pageLoaded = pageLoaded;

function addNinja(image) {
    return new Promise(function (resolve, reject) {

        if(ninja != undefined){
           world.removeBody(ninja);
        }

        ninja = Physics.body('circle', {
            x: 70,
            y: 280,
            radius: 35,
            styles: { image: image }
        });

        world.add(ninja);
        Ninja = ninja;
        resolve(ninja);
    });
}

function triggerGame(args) {
    switch (args.direction) {
        case 1:
            console.log("right");
            break;
        case 2:
            console.log("left");
            break;
        case 4:
            console.log("up");
            break;
        case 8:
            console.log("down");
            break;
        default:
            console.log("default swipe");
    }
}

exports.triggerGame = triggerGame;
memePushpendu commented 7 years ago

OUTPUT
https://jsplayground.000webhostapp.com/sapp/WhatsApp%20Image%202017-08-19%20at%209.50.34%20AM.jpeg

memePushpendu commented 7 years ago
 if(Ninja != undefined){
       world.removeBody(ninja);
    }

same output