phina.js cannot get a winning decision

Asked 2 years ago, Updated 2 years ago, 167 views

phina.js Tips Collection #WinningDetermination - Qiita

Using the above site as a reference, I am thinking of using the phina.js library to avoid the square coming from all directions using the code below, but line 100

if(shape.hitTestElement(sprite)){
              varself = this;
              self.exit();
            }

When you add them, "ReferenceError: sprite is not defined" appears on the console and does not work.Except for the above code, it works fine.

In the init property, sprite seems to be defined, but what is the problem?
Please tell me how to deal with it.Thank you for your cooperation.

environment

OS:windows10
browsers:firefox

Code of interest

<html>
  <head>
    <metacharset='utf-8'/>
    <meta name="viewport" content="width=device-width, user-scalable=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>

    <title> Getting started | phina.js</title>
    <!--Read phina.js-->
    <script src='http://cdn.rawgit.com/phi-jp/phina.js/v0.2.0/build/phina.js'></script>

    <!--Main Action-->
    <script>
      // Deploy phina.js to Global Territories
phina.globalize();

varASSETS = {
  // image
  image:{
    'tomapiko': 'https://rawgit.com/phi-jp/phina.js/develop/assets/images/tomapiko.png',
  },
};

var SCREEN_WIDTH = 640;
var SCREEN_HEIGHT=960;
varSPEED=5;
varENEMY_INTERVAL = 10;

// Define MainScene Class
phina.define('MainScene', {
  superClass: 'DisplayScene',
  init:function(){
    This.superInit();
    // Specify background color
    This.backgroundColor='#444';
    // sprite imaging
    varsprite=Sprite('tomapiko').addChildTo(this);
    // initial position
    sprite.x = 325;
    sprite.y = 740;

      // after a certain frame has passed
    // Touch Retention Event
    This.onpointstay=function(e){
      // Place the sprite in the touch position
      sprite.x = e.pointer.x;
      sprite.y = e.pointer.y;
    };
    // Touch Move Event
    This.onpointmove=function(e){
      // Place the sprite in the touch position
      sprite.x = e.pointer.x;
      sprite.y = e.pointer.y;
    };

  },

  update:function(app){

      if(app.frame%ENEMY_INTERVAL===0){
          // enemy generation
          // Draw a rectangle
        for(varkali=1;kali<=50;){
          var default_x = Random.randint (-15,655);
          var default_y = Random.randint (-15,975);
          if(-5<=default_x&&default_x<=645&-5<=default_y&=965){}
          else{
            varshape=Shape().addChildTo(this);
            shape.setSize(10,10);
            shape.x = default_x;
            shape.y = default_y;

            if(-15<=default_x&&default_x<=320&&-15<=default_y&&default_y<=330){
              // Apply Physical Class and Move
              varphis_y_left_up = Random.randint(1,4)
              varphis_x_left_up = Math.sqrt(25-phis_y_left_up^2)
              var last_x = phis_x_left_up
              var last_y=phis_y_left_up
            }
            else if (321<=default_x&&default_x<=655&-15<=default_y&=330){
              varphis_x_right_up = Random.randint(-1,-4)
              varphis_y_right_up = Math.sqrt(25-phis_x_right_up^2)
              var last_x = phis_x_right_up
              var last_y = phis_y_right_up
            }
            else if (-15<=default_x&&default_x<=320&331<=default_y&&default_y<=975){
              varphis_y_left_down = Random.randint(-1,-4)
              varphis_x_left_down = Math.sqrt(25-phis_y_left_down^2)
              var last_x = phis_x_left_down
              var last_y = phis_y_left_down
            }
            else if (321<=default_x&&default_x<=655&331<=default_y&&default_y<=975){
              varphis_x_right_down = Random.randint(-1,-4)
              varkali_k = Math.sqrt(25-phis_x_right_down^2)
              varphis_y_right_down = kali_k*-1
              var last_x = phis_x_right_down
              var last_y = phis_y_right_down
            }
            shape.physical.force(last_x, last_y);
            if(shape.hitTestElement(sprite)){
              varself = this;
              self.exit();
            }

          }
          kali++;
        }
      }
  }

});

// main processing
phina.main(function(){
  // application generation
  var app = GameApp({
    // startLabel: 'main', // Start with main scene
    assets —ASSETS, // loading image
  });
  // application execution
  app.run();
});

    </script>
  </head>
  <body>

  </body>
</html>

javascript html html5 game-development

2022-09-30 19:24

1 Answers

 init:function(){
    // 
    // sprite imaging
    varsprite=Sprite('tomapiko').addChildTo(this);
    // initial position
    sprite.x = 325;
    sprite.y = 740;

The variable sprite defined here is the local variable for this function.It cannot be viewed from outside the function that is set to init.Therefore, if you try to browse among the functions that you set for the update property, you get an error.

The workaround is to define sprite in a location that can be referenced by the function that you set to the update property.


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.