Jump to content

I have a problem. when I press the button, the animation doesn't loop.


zouarino
 Share

Recommended Posts

I have a problem. when I press the button, the animation doesn't loop.
please help my.
 
 
 var createScene = function () {
    var scene = new BABYLON.Scene(engine);
 
    var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 8, 50, BABYLON.Vector3.Zero(), scene);
 
    camera.attachControl(canvas, false);
 
    var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
       
     BABYLON.SceneLoader.ImportMesh("Cube", "scenes/", "x.babylon", scene, function (newMeshes) {
      
scene.stopAnimation(newMeshes[0]);
 
                  // On push on a touch...
window.addEventListener('keydown',function(event){
 
if (event.keyCode == 65)
{
     scene.beginAnimation(newMeshes[0], 40, 60, true); 
}
 
});        
// On push off a touch...
window.addEventListener('keyup',function(event){
if (event.keyCode == 65)
{
     scene.stopAnimation(newMeshes[0]);
});
     
 });
 
    return scene;
}
   

 

Link to comment
Share on other sites

Hi;

 

First of all, as Deltakosh said, a quick repo in the Playground would be more flexible and an easy way to debug your case.

btw, your problem comes from your implementation of the "addeventlistener". Mesh doesn't support such method.

 

Here is a quick solution, where I used the pointerdown and pointerup event attached on the canvas, and the scene.pick() method to know if we clicked the correct mesh, or not :

 

Seb aka Mimetis

// Get the canvas element from our HTML belowvar canvas = document.getElementById("renderCanvas");// Load the BABYLON 3D enginevar engine = new BABYLON.Engine(canvas, true);var meshAnimation = undefined;// -------------------------------------------------------------var createScene = function () {    var scene = new BABYLON.Scene(engine);    var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 8, 50, BABYLON.Vector3.Zero(), scene);    camera.attachControl(canvas, false);    var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);    BABYLON.SceneLoader.ImportMesh("Cube", "/scenes/", "x.babylon", scene, function (newMeshes) {        meshAnimation = newMeshes[0];        scene.stopAnimation(meshAnimation);    }, function (scene) {        console.log(scene.toString());    });    return scene;}// -------------------------------------------------------------var onPointerDown = function (evt) {    if (evt.button !== 0 || meshAnimation === undefined) {        return;    }    // check if we are under a mesh    var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh === meshAnimation; });    if (pickInfo.hit) {        scene.beginAnimation(meshAnimation, 40, 60, true);    }}var onPointerUp = function () {    if (meshAnimation === undefined) {        return;    }    // check if we are under a mesh    var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh === meshAnimation; });    if (pickInfo.hit) {        scene.stopAnimation(meshAnimation);    }}canvas.addEventListener("pointerdown", onPointerDown, false);canvas.addEventListener("pointerup", onPointerUp, false);// Now, call the createScene function that you just finished creatingvar scene = createScene();scene.onDispose = function () {    canvas.removeEventListener("pointerdown", onPointerDown);    canvas.removeEventListener("pointerup", onPointerUp);}// Register a render loop to repeatedly render the sceneengine.runRenderLoop(function () {    scene.render();});// Watch for browser/canvas resize eventswindow.addEventListener("resize", function () {    engine.resize();});
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...