用JS怎么做一个自动跟踪角色的功能?

JavaScript010

用JS怎么做一个自动跟踪角色的功能?,第1张

你可以使用JavaScript来实现这样的自动跟踪功能,主要步骤如下:

1、获取角色和怪物的坐标位置:使用JavaScript的getBoundingClientRect()方法获取角色和怪物的坐标位置。

2、计算角色和怪物的距离:使用两点间距离公式计算角色和怪物的距离。

3、判断角色是否在怪物跟踪范围内:当角色和怪物的距离小于一个特定的跟踪范围时,表示角色在怪物跟踪范围内。

4、设置角色跟踪移动速度:设置怪物每次移动的速度,使它能够朝向角色并靠近。

5、在每一帧动画中执行跟踪动作:使用JavaScript的requestAnimationFrame()方法,在每一帧动画中执行跟踪动作。

下面是一个示例代码,它展示了如何实现自动跟踪功能:

// 获取角色和怪物的坐标位置

var characterRect = character.getBoundingClientRect()

var monsterRect = monster.getBoundingClientRect()

// 计算角色和怪物的距离

var distance = Math.sqrt(Math.pow(characterRect.x - monsterRect.x, 2) + Math.pow(characterRect.y - monsterRect.y, 2))

// 判断角色是否在怪物跟踪范围内

if (distance <TRACKING_RANGE) {

// 设置角色跟踪移动速度

var speedX = (characterRect.x - monsterRect.x) / distance

var speedY = (characterRect.y - monsterRect.y) / distance

// 在每一帧动画中执行跟踪动作

function trackCharacter() {

// 获取当前角色和怪物的坐标位置

characterRect = character.getBoundingClientRect()

monsterRect = monster.getBoundingClientRect()

// 计算角色和怪物的距离

distance = Math.sqrt(Math.pow(characterRect.x - monsterRect.x, 2) + Math.pow(characterRect.y - monsterRect.y, 2))

// 判断角色是否在怪物跟踪范围内

if (distance <TRACKING_RANGE) {

// 更新怪物的坐标位置

monster.style.left = monsterRect.x + speedX + "px"

monster.style.top = monsterRect.y + speedY + "px"

// 继续跟踪角色

requestAnimationFrame(trackCharacter)

}

}

trackCharacter()

}

移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成。但是在移动设备上,要实现这种轮播的效果,就需要用到核心的touch事件。处理touch事件能跟踪到屏幕滑动的每根手指。

以下是四种touch事件 touchstart:     //手指放到屏幕上时触发

touchmove:      //手指在屏幕上滑动式触发

touchend:    //手指离开屏幕时触发

touchcancel:     //系统取消touch事件的时候触发,这个好像比较少用

每个触摸事件被触发后,会生成一个event对象,event对象里额外包括以下三个触摸列表

ouches:     //当前屏幕上所有手指的列表targetTouches:      //当前dom元素上手指的列表,尽量使用这个代替touches

changedTouches:     //涉及当前事件的手指的列表,尽量使用这个代替touches

这些列表里的每次触摸由touch对象组成,touch对象里包含着触摸信息,主要属性如下:

clientX / clientY:      //触摸点相对浏览器窗口的位置

pageX / pageY:       //触摸点相对于页面的位置

screenX  /  screenY:    //触摸点相对于屏幕的位置

identifier:        //touch对象的ID

target:       //当前的DOM元素 注意:

手指在滑动整个屏幕时,会影响浏览器的行为,比如滚动和缩放。所以在调用touch事件时,要注意禁止缩放和滚动。

1.禁止缩放

通过meta元标签来设置。

<meta name="viewport" content="target-densitydpi=320,width=640,user-scalable=no"> 2.禁止滚动

preventDefault是阻止默认行为,touch事件的默认行为就是滚动。

event.preventDefault() 案例:

下面给出一个案例,需在移动设备上才能看出效果。

1.定义touchstart的事件处理函数,并绑定事件:

if(!!self.touch) self.slider.addEventListener('touchstart',self.events,false)

//定义touchstart的事件处理函数

start:function(event){

var touch = event.targetTouches[0] //touches数组对象获得屏幕上所有的touch,取第一个touch

startPos = {x:touch.pageX,y:touch.pageY,time:+new Date} //取第一个touch的坐标值

isScrolling = 0 //这个参数判断是垂直滚动还是水平滚动

this.slider.addEventListener('touchmove',this,false)

this.slider.addEventListener('touchend',this,false)

},

触发touchstart事件后,会产生一个event对象,event对象里包括触摸列表,获得屏幕上的第一个touch,并记下其pageX,pageY的坐标。定义一个变量标记滚动的方向。此时绑定touchmove,touchend事件。

2.定义手指在屏幕上移动的事件,定义touchmove函数。

//移动

move:function(event){

//当屏幕有多个touch或者页面被缩放过,就不执行move操作

if(event.targetTouches.length > 1 || event.scale && event.scale !== 1) return

var touch = event.targetTouches[0]

endPos = {x:touch.pageX - startPos.x,y:touch.pageY - startPos.y}

isScrolling = Math.abs(endPos.x) < Math.abs(endPos.y) ? 1:0 //isScrolling为1时,表示纵向滑动,0为横向滑动

if(isScrolling === 0){

event.preventDefault() //阻止触摸事件的默认行为,即阻止滚屏

this.slider.className = 'cnt'

this.slider.style.left = -this.index*600 + endPos.x + 'px'

}

},

同样首先阻止页面的滚屏行为,touchmove触发后,会生成一个event对象,在event对象中获取touches触屏列表,取得第一个touch,并记下pageX,pageY的坐标,算出差值,得出手指滑动的偏移量,使当前DOM元素滑动。

3.定义手指从屏幕上拿起的事件,定义touchend函数。

//滑动释放

end:function(event){

var duration = +new Date - startPos.time //滑动的持续时间

if(isScrolling === 0){ //当为水平滚动时

this.icon[this.index].className = ''

if(Number(duration) > 10){ 

//判断是左移还是右移,当偏移量大于10时执行

if(endPos.x > 10){

if(this.index !== 0) this.index -= 1

}else if(endPos.x < -10){

if(this.index !== this.icon.length-1) this.index += 1

}

}

this.icon[this.index].className = 'curr'

this.slider.className = 'cnt f-anim'

this.slider.style.left = -this.index*600 + 'px'

}

//解绑事件

this.slider.removeEventListener('touchmove',this,false)

this.slider.removeEventListener('touchend',this,false)

},

手指离开屏幕后,所执行的函数。这里先判断手指停留屏幕上的时间,如果时间太短,则不执行该函数。再判断手指是左滑动还是右滑动,分别执行不同的操作。最后很重要的一点是移除touchmove,touchend绑定事件。

常规的JS页面跳转代码

1、在原来的窗体中直接跳转用

<script type="text/javascript">

window.location.href="你所要跳转的页面"

</script>

2、在新窗体中打开页面用:

<script type="text/javascript">

window.open('你所要跳转的页面')

</script>

3、JS页面跳转参数的注解

<SCRIPT LANGUAGE="javascript">

<!--

window.open ('page.html', 'newwindow', 'height=100, width=400, top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')

//写成一行

-->

</SCRIPT>

参数解释:

<SCRIPT LANGUAGE="javascript">js脚本开始;

window.open 弹出新窗口的命令;

'page.html' 弹出窗口的文件名;

'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空'代替;

height=100 窗口高度;

width=500 窗口宽度;

top=0 窗口距离屏幕上方的象素值;

left=0 窗口距离屏幕左侧的象素值。