Actionscript 3 在AS3,Box2d中,资产的移动速度比其父实体更快
我正在尝试创建一个滑雪板,它可以在斜坡上冲浪。但当我运行项目时,我添加的资产运行速度比它的父实体快。它只是超出了身体的界限。为什么即使在设置了相对于父实体的位置后,也会发生这种情况。这是我使用的代码。更不用说我是Box2d新手Actionscript 3 在AS3,Box2d中,资产的移动速度比其父实体更快,actionscript-3,box2d,flash-cs5,Actionscript 3,Box2d,Flash Cs5,我正在尝试创建一个滑雪板,它可以在斜坡上冲浪。但当我运行项目时,我添加的资产运行速度比它的父实体快。它只是超出了身体的界限。为什么即使在设置了相对于父实体的位置后,也会发生这种情况。这是我使用的代码。更不用说我是Box2d新手 package { import flash.display.MovieClip; import flash.display.Sprite; import Box2D.Collision.*; impo
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import Box2D.Collision.*;
import Box2D.Common.*;
import Box2D.Dynamics.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.Math.*;
import Box2D.Collision.Shapes.*;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.sampler.NewObjectSample;
import flash.display.Shape;
public class PhysSnowboarder extends Sprite {
//Degree to Radian Constant
private const degreesToRadians:Number=0.0174532925;
//World Definition
public var worldScale:Number=30;
private var world:b2World= new b2World(new b2Vec2(0,9.8),true);
//Key Flags (Game Control Keys)
private var left:Boolean=false;
private var right:Boolean=false;
//Hill attributes and misc
private var nextHill:Number=240;
private var realHeight:Number=240;
private var realWidth:Number=0;
private var buildNextHillAt:Number=0;
//Sphere
var theSphere:b2Body;
var riderBody:b2Body;
//Game Canvas
private var gameCanvas:Sprite= new Sprite();
public function PhysSnowboarder(){
// i created a simple image of a snowboarder, created an as linkage.
// Inherits flash.display.sprite
var boardRider:BoardRider= new BoardRider();
//create a sphere
var sphereShape:b2CircleShape= new b2CircleShape(50/worldScale);
var sphereFixture:b2FixtureDef= new b2FixtureDef();
sphereFixture.density=2;
sphereFixture.friction=3;
sphereFixture.restitution=0.1;
//sphereFixture.filter.groupIndex=-1;
sphereFixture.shape=sphereShape;
//sphere body
var sphereBodyDef:b2BodyDef= new b2BodyDef();
sphereBodyDef.type=b2Body.b2_dynamicBody;
sphereBodyDef.position.Set(320/worldScale,0);
sphereBodyDef.userData=new Object();
sphereBodyDef.userData.asset=boardRider;
//Add the sphere to stage
theSphere=world.CreateBody(sphereBodyDef);
theSphere.SetUserData(sphereBodyDef.userData);
theSphere.CreateFixture(sphereFixture);
addChild(sphereBodyDef.userData.asset);
//theSphere.SetUserData(boardRider);
//addChild(theSphere.GetUserData());
addChild(gameCanvas);
//stage.addChild(new ());
debugDraw();
while(realWidth<1280){
nextHill=drawHill(5,realWidth,nextHill);
}
addEventListener(Event.ENTER_FRAME,updateWorld);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP,keyReleased);
}
//debugDraw
private function debugDraw():void{
var worldDebugDraw:b2DebugDraw= new b2DebugDraw();
var debugSprite:Sprite= new Sprite();
gameCanvas.addChild(debugSprite);
worldDebugDraw.SetSprite(debugSprite);
worldDebugDraw.SetDrawScale(worldScale);
worldDebugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);
worldDebugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(worldDebugDraw);
}
//drawHill
private function drawHill(pixelStep:int,xOffset:Number,yOffset:Number):Number{
var hillStartY:Number=yOffset;
var hillWidth:Number=120+Math.ceil(Math.random()*26)*20;
realWidth+=hillWidth;
var numberOfSlices=hillWidth/pixelStep;
var hillVector:Vector.<b2Vec2>;
var randomHeight:Number;
if(xOffset==0){
randomHeight=0;
}
else{
do{
//randomHeight=Math.random()*hillWidth/7.5;
randomHeight=240/worldScale;
}while(realHeight+randomHeight>600);
}
realHeight+=randomHeight;
hillStartY-=randomHeight;
for (var j:int=0; j<numberOfSlices/2; j++) {
hillVector=new Vector.<b2Vec2>();
hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,480/worldScale));
hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*j))/worldScale));
hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*(j+1)))/worldScale));
hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,480/worldScale));
var sliceBody:b2BodyDef=new b2BodyDef ;
var centre:b2Vec2=findCentroid(hillVector,hillVector.length);
sliceBody.position.Set(centre.x,centre.y);
for (var z:int=0; z<hillVector.length; z++) {
hillVector[z].Subtract(centre);
}
var slicePoly:b2PolygonShape=new b2PolygonShape ;
slicePoly.SetAsVector(hillVector,4);
var sliceFixture:b2FixtureDef=new b2FixtureDef ;
sliceFixture.shape=slicePoly;
var worldSlice:b2Body=world.CreateBody(sliceBody);
worldSlice.CreateFixture(sliceFixture);
}
hillStartY-=randomHeight;
if (xOffset==0) {
randomHeight=0;
}
else {
do {
randomHeight=Math.random()*hillWidth/5;
} while (realHeight-randomHeight<240);
}
realHeight-=randomHeight;
hillStartY+=randomHeight;
for (j=numberOfSlices/2; j<numberOfSlices; j++) {
hillVector=new Vector.<b2Vec2>();
hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,480/worldScale));
hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*j))/worldScale));
hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*(j+1)))/worldScale));
hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,480/worldScale));
sliceBody=new b2BodyDef ;
centre=findCentroid(hillVector,hillVector.length);
sliceBody.position.Set(centre.x,centre.y);
for (z=0; z<hillVector.length; z++) {
hillVector[z].Subtract(centre);
}
slicePoly=new b2PolygonShape ;
slicePoly.SetAsVector(hillVector,4);
sliceFixture=new b2FixtureDef ;
sliceFixture.shape=slicePoly;
worldSlice=world.CreateBody(sliceBody);
worldSlice.CreateFixture(sliceFixture);
}
hillStartY=hillStartY+randomHeight;
return (hillStartY);
}
//findCentroid
private function findCentroid(vs:Vector.<b2Vec2>,count:uint):b2Vec2{
var c:b2Vec2= new b2Vec2();
var area:Number=0.0;
var p1X:Number=0.0;
var p1Y:Number=0.0;
var inv3:Number=1.0/3.0;
for(var i:int=0; i<count;++i){
var p2:b2Vec2=vs[i];
var p3:b2Vec2=i+1<count?vs[int(i+1)]:vs[0];
var e1X:Number=p2.x-p1X;
var e1Y:Number=p2.y-p1Y;
var e2X:Number=p3.x-p1X;
var e2Y:Number=p3.y-p1Y;
var D:Number=(e1X*e2Y-e1Y*e2X);
var triangleArea:Number=0.5*D;
area+=triangleArea;
c.x+=triangleArea*inv3*(p1X+p2.x+p3.x);
c.y+=triangleArea*inv3*(p1Y+p2.y+p3.y);
}
c.x*=1.0/area;
c.y*=1.0/area;
return c;
}
//updateWorld
private function updateWorld(e:Event):void{
if(left)
{
//theSphere.SetLinearVelocity(new b2Vec2(-10,0));
theSphere.ApplyTorque(-3);
}
if(right)
{
//theSphere.SetLinearVelocity(new b2Vec2(10,0));
theSphere.ApplyTorque(3);
//theSphere.ApplyTorque(0.5);
}
world.Step(1/30,10,10);
world.ClearForces();
for(var currentBody:b2Body=world.GetBodyList(); currentBody; currentBody=currentBody.GetNext())
{
if(currentBody.GetUserData()!=null){
gameCanvas.x=320-currentBody.GetPosition().x*worldScale;
gameCanvas.y=240-currentBody.GetPosition().y*worldScale;
//gameCanvas.addChild(currentBody.GetUserData());
if(gameCanvas.x<=-realWidth+800){
nextHill=drawHill(5,realWidth,nextHill);
}
//This is where i set the position of the of the asset i created.
currentBody.GetUserData().asset.x=(currentBody.GetPosition().x*worldScale);
currentBody.GetUserData().asset.y=((currentBody.GetPosition().y+(50/worldScale))*worldScale);
}
if(currentBody.GetPosition().x*worldScale<(gameCanvas.x*-1)-640){
world.DestroyBody(currentBody);
}
}
world.DrawDebugData();
}
//keyPressed
private function keyPressed(e:KeyboardEvent):void {
switch (e.keyCode) {
case 37 :
left=true;
break;
case 39 :
right=true;
break;
}
}
//keyReleased
private function keyReleased(e:KeyboardEvent):void {
switch (e.keyCode) {
case 37 :
left=false;
break;
case 39 :
right=false;
break;
}
}
}//Class Ends here
}//Package Ends here
包{
导入flash.display.MovieClip;
导入flash.display.Sprite;
导入Box2D.Collision.*;
导入Box2D.Common.*;
导入Box2D.Dynamics.*;
导入Box2D.Dynamics.Joints.*;
导入Box2D.Dynamics.Contacts.*;
导入Box2D.Common.Math.*;
导入Box2D.Collision.Shapes.*;
导入flash.events.Event;
导入flash.events.KeyboardEvent;
导入flash.sampler.NewObjectSample;
导入flash.display.Shape;
公共类物理导航器扩展Sprite{
//弧度常数
私有常数度弧度:数字=0.0174532925;
//世界定义
公共var worldScale:数字=30;
私有风险值世界:b2World=新b2World(新b2Vec2(0,9.8),真);
//键标志(游戏控制键)
私有变量left:Boolean=false;
private-var-right:Boolean=false;
//希尔属性和杂项
私有变量nextHill:Number=240;
私有变量realHeight:Number=240;
私有变量realWidth:Number=0;
私有var buildNextHillAt:Number=0;
//球体
变形金刚:b2Body;
var riderBody:b2Body;
//游戏画布
private var gameCanvas:Sprite=new Sprite();
公共职能部门医务人员(){
//我创建了一个滑雪板的简单图像,创建了一个as链接。
//继承flash.display.sprite
var boardRider:boardRider=新的boardRider();
//创建一个球体
var sphereShape:b2CircleShape=新的b2CircleShape(50/世界范围);
var sphereFixture:b2FixtureDef=new b2FixtureDef();
密度=2;
sphereFixture.摩擦力=3;
sphereFixture.restitution=0.1;
//sphereFixture.filter.groupIndex=-1;
sphereFixture.shape=sphereShape;
//球体
var sphereBodyDef:b2BodyDef=新的b2BodyDef();
sphereBodyDef.type=b2Body.b2_dynamicBody;
sphereBodyDef.位置设置(320/worldScale,0);
sphereBodyDef.userData=新对象();
sphereBodyDef.userData.asset=boardRider;
//将球体添加到舞台
theSphere=world.CreateBody(sphereBodyDef);
theSphere.SetUserData(sphereBodyDef.userData);
CreateFixture(sphereFixture);
addChild(sphereBodyDef.userData.asset);
//theSphere.SetUserData(boardRider);
//addChild(theSphere.GetUserData());
addChild(游戏画布);
//stage.addChild(新的());
debugDraw();
而(600),;
}
realHeight+=随机高度;
山坡-=平均高度;
对于(var j:int=0;jp请仅发布相关代码这是一个现成的代码。我不希望其他人键入它并在这方面花费太多时间。他们可以简单地复制粘贴代码链接到资产并运行它。:)