简体   繁体   English

如何使用JavaScript使角色随机移动

[英]How to make my character move randomly using JavaScript

var dx:Number = 0;
var dy:Number = 0;
var target:Point;
var Speed:Number = 10;

target = new Point(Math.random() * 500, Math.random() * 400);

trace(target);

stage.addEventListener(Event.ENTER_FRAME, FollowBall);

function FollowBall(event: Event):void {
  var angle = Math.atan2(dy, dx) / Math.PI * 180;

  Mozzie.rotation = angle;

  dx = Mozzie.x - target.x;
  dy = Mozzie.y - target.y;
  Mozzie.x -= dx / Speed;
  Mozzie.y -= dy / Speed;

  var hyp = Math.sqrt((dx * dx) + (dy * dy));

  if (hyp <= 20) {
    target.x = Math.random() * 1000;
    target.y = Math.random() * 600;
  }
}

Previously, I am using ActionScript 3.0 to do my animation, but now I have to convert my code to JavaScript. 以前,我使用ActionScript 3.0制作动画,但是现在我必须将代码转换为JavaScript。

And I know nothing about JavaScript, I need some help here. 而且我对JavaScript一无所知,在这里需要一些帮助。

Well, this is a starting point. 好吧,这是一个起点。 I converted the AS3 into ES6 JavaScript with minimal cosmetic changes to your existing code. 我将AS3转换为ES6 JavaScript,并且对现有代码进行了最少的外观更改。 You can see how very similar they are. 您可以看到它们有多么相似。

Unless you are using third party ActionScript libraries, I believe that over 90% of you logic will port over well. 除非您使用第三方ActionScript库,否则我相信您90%以上的逻辑都可以很好地移植。 Although AS3 uses the ECMAScript4, it was leagues ahead of the ES4 and ES5 implementation standardization in JavaScript. 尽管AS3使用ECMAScript4,但它比JavaScript中的ES4和ES5实现标准化领先。

You will need to tweak the event handler because I am uncertain how Event.ENTER_FRAME works, it's been a while since I worked with Flex. 您将需要调整事件处理程序,因为我不确定Event.ENTER_FRAME工作方式,因为我使用Flex已经有一段时间了。 I assume it's some kind of mouse event. 我认为这是某种鼠标事件。 As for the trace, I just mimicked it's functionality by slicing the incoming arguments, because they can be comma-delimited in Flash; 至于跟踪,我只是通过切入传入参数来模仿它的功能,因为它们可以在Flash中用逗号分隔。 and just map-joined them. 然后将他们加入地图

I highly recommend switching over to JavaScript right away. 我强烈建议立即切换到JavaScript。 Flex/AS/Flash is a dying language and ES6 + HTML5 is the new way of life. Flex / AS / Flash是一种垂死的语言,ES6 + HTML5是一种新的生活方式。

 // ===================================================================== // Setup // ===================================================================== class Point { constructor(x, y) { this.x = x; this.y = y; } } class Mozzie { } Mozzie.x = 0; Mozzie.y = 0; Mozzie.rotation = 0; function trace() { console.log([].slice.call(arguments).map(x => JSON.stringify(x)).join(' ')); } var stage = document.getElementById('stage'); // ===================================================================== var dx = 0; var dy = 0; var target; // Point var Speed = 10; target = new Point(Math.random() * 500, Math.random() * 400); trace(target); stage.addEventListener('mouseenter', FollowBall); function FollowBall(event) { var angle = Math.atan2(dy, dx) / Math.PI * 180; Mozzie.rotation = angle; dx = Mozzie.x - target.x; dy = Mozzie.y - target.y; Mozzie.x -= dx / Speed; Mozzie.y -= dy / Speed; var hyp = Math.sqrt((dx * dx) + (dy * dy)); if (hyp <= 20) { target.x = Math.random() * 1000; target.y = Math.random() * 600; } } 
 #stage { width: 500px; height: 400px; border: thin solid black; } 
 <div id="stage"></div> 

Helpful Links 有用的网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM