有没有人有任何例子/知道任何教程,可以让我在flash ActionScript3中使用对象进行侧面碰撞?我一直在找,但什么也找不到。谢谢你的帮助。
发布于 2011-04-06 09:44:08
我相信您正在寻找的是可用于所有显示对象的"hitTest“函数。hitTestObject会告诉您两个对象是否发生了碰撞,而hitTestPoint会告诉您对象是否与点发生了碰撞。假设有一个名为“myDisplayObj”的MovieClip变量:
if( myDisplayObj.hitTestObject(otherDisplayObj) ){ //do object collision code }
if( myDisplayObj.hitTestPoint(100,350) ){ //do point collision code }发布于 2011-04-06 11:42:24
试试这个..。
实现是,checkForCollision()将返回一个字符串来表示您所碰撞的那一方。要检查其是否发生冲突的对象需要扩展此类。此外,如果要使用有角度的平铺,请将angl属性设置为tl、bl、tr、tl之一,以表示倾斜边的位置。希望这不会太让人困惑。
public class Impassable extends MovieClip
{
// Vars
public var angl:String = "";
/**
* Checks if the specified point collides with this
* @param cx The x value of the point being checked
* @param cy The y value of the point being checked
* @param offset An offset from the edges of this that can be considered as part of the radius of this
* @return A String representing the side that a collision was detected on
*/
public function checkForCollision(cx:int, cy:int, offset:int=0):String
{
if(angl.length < 1)
{
// Horizontal
if(cy > y - 1 && cy < y + height + 1)
{
if(cx > x - offset && cx < x + width/2) return 'WEST';
if(cx < x + width + offset && cx > x + width/2) return 'EAST';
}
// Vertical
if(cx > x - 1 && cx < x + height + 1)
{
if(cy > y - offset && cy < y + height/2) return 'NORTH';
if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
}
}
else
{
// Gradient (1)
var xgr:Number = cx - x;
var ygr:Number = cy - y;
var ua:Boolean = false;
// Angled Tiles
if(angl == "tl")
{
// Top Left
if(cx > x - 1 && cy > y - 1) ua = true;
if(ua)
{
// Angle Collision
if(cy < y + height - xgr + offset || cx < x + width - ygr + offset) return 'SOUTH_EAST';
}
else
{
// Straight Collision
if(cy > y - 1 && cy < y + height + 1)
{
if(cx > x - offset && cx < x + width/2) return 'WEST';
}
if(cx > x - 1 && cx < x + height + 1)
{
if(cy > y - offset && cy < y + height/2) return 'NORTH';
}
}
}
if(angl == "tr")
{
// Top Right
if(cx < x + width + 1 && cy > y - 1) ua = true;
if(ua)
{
// Angle Collision
if(cy < y + height - (width - xgr) + offset || cx > x + ygr - offset) return 'SOUTH_WEST';
}
else
{
// Straight Collision
if(cy > y - 1 && cy < y + height + 1)
{
if(cx < x + width + offset && cx > x + width/2) return 'EAST';
}
if(cx > x - 1 && cx < x + height + 1)
{
if(cy > y - offset && cy < y + height/2) return 'NORTH';
}
}
}
if(angl == "br")
{
// Bottom Right
if(cx < x + width + 1 && cy < y + height + 1) ua = true;
if(ua)
{
// Angle Collision
if(cx > x + (height - ygr) - offset || cy > y + height - xgr - offset) return 'NORTH_WEST';
}
else
{
// Straight Collision
if(cy > y - 1 && cy < y + height + 1)
{
if(cx < x + width + offset && cx > x + width/2) return 'EAST'
}
if(cx > x - 1 && cx < x + height + 1)
{
if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
}
}
}
if(angl == "bl")
{
// Bottom Left
if(cx > x - 1 && cy < y + height + 1) ua = true;
if(ua)
{
// Angle Collision
if(cx < x + ygr + offset || cy > y + xgr - offset) return 'NORTH_EAST';
}
else
{
// Straight Collision
if(cy > y - 1 && cy < y + height + 1)
{
if(cx > x - offset && cx < x + width/2) return 'WEST';
}
if(cx > x - 1 && cx < x + height + 1)
{
if(cy < y + height + offset && cy > y + height/2) return 'SOUTH';
}
}
}
}
return "";
}
}https://stackoverflow.com/questions/5560543
复制相似问题