Commit 7da17f99 authored by 汪欢's avatar 汪欢

a

parent d3d3c9d7
...@@ -114,8 +114,14 @@ ...@@ -114,8 +114,14 @@
_this.toRo = 180 / Math.PI; _this.toRo = 180 / Math.PI;
_this.loopMark = false; _this.loopMark = false;
_this.guideMark = false; _this.guideMark = false;
_this.ballAddSpeedX = 1.2; _this.inPointMark = false;
_this.ballAddSpeedY = 5.3; _this.outPointMark = false;
_this.boardMark = false;
_this.pointMark = false;
_this.planeMark = false;
_this.stringMark = "";
_this.ballAddSpeedX = -40;
_this.ballAddSpeedY = -80;
_this.gravity = -5; _this.gravity = -5;
_this.stepSpeed = [0.5, 3.5]; _this.stepSpeed = [0.5, 3.5];
_this.detaTimeEf = _this.stepSpeed[1]; _this.detaTimeEf = _this.stepSpeed[1];
...@@ -130,7 +136,27 @@ ...@@ -130,7 +136,27 @@
_this.clickMark = false; _this.clickMark = false;
_this.once(engine.Event.ADDED_TO_STAGE, _this.start, _this); _this.once(engine.Event.ADDED_TO_STAGE, _this.start, _this);
var p2 = window["p2"]; var p2 = window["p2"];
var materialBall = new p2.Material(1); _this.world = new p2.World();
_this.world.sleepMode = p2.World.BODY_SLEEPING;
_this.world.gravity = [0, 10];
var box = new p2.Box({
width: 20,
height: 200,
material: materialBoard,
});
_this.boxBody = new p2.Body({
mass: 0,
type: p2.Body.STATIC,
position: [20, 400],
});
_this.boxBody.addShape(box);
_this.box = _this.createBox(0, -100, 20, 200);
_this.boxBody.displays = [_this.box];
_this.world.addBody(_this.boxBody);
_this.world.defaultContactMaterial.friction = 0.1;
_this.world.defaultContactMaterial.restitution = 1;
_this.world.setGlobalStiffness(1e5);
var materialBall = new p2.Material(2);
var materialPoint = new p2.Material(2); var materialPoint = new p2.Material(2);
var materialBoard = new p2.Material(2); var materialBoard = new p2.Material(2);
var materialPlane = new p2.Material(2); var materialPlane = new p2.Material(2);
...@@ -139,50 +165,77 @@ ...@@ -139,50 +165,77 @@
restitution: 0.5, restitution: 0.5,
}); });
var contactMaterialBallBoard = new p2.ContactMaterial(materialBall, materialBoard, { var contactMaterialBallBoard = new p2.ContactMaterial(materialBall, materialBoard, {
friction: 0.3, friction: 0.1,
restitution: 0.5, restitution: 1,
}); });
var contactMaterialBallPlane = new p2.ContactMaterial(materialBall, materialPlane, { var contactMaterialBallPlane = new p2.ContactMaterial(materialBall, materialPlane, {
friction: 0.3, friction: 0.1,
restitution: 0.7, restitution: 1,
}); });
_this.world = new p2.World();
_this.world.sleepMode = p2.World.BODY_SLEEPING;
_this.world.gravity = [0, 10];
var box = new p2.Box({ width: _this.config.boardBody.width, height: _this.config.boardBody.height, material: materialBoard });
_this.boxBody = new p2.Body({ mass: 0, type: p2.Body.STATIC, position: [20, 200] });
_this.boxBody.addShape(box);
var display = new engine.Texture(getTextureByName('篮板'));
var displays = new engine.Sprite(display);
_this.addChild(displays);
_this.boxBody.displays = [displays];
_this.world.addBody(_this.boxBody);
_this.world.defaultContactMaterial.friction = 0.3;
_this.world.defaultContactMaterial.restitution = 0.6;
_this.world.setGlobalStiffness(1e5);
_this.world.addContactMaterial(contactMaterialBallPoint); _this.world.addContactMaterial(contactMaterialBallPoint);
_this.world.addContactMaterial(contactMaterialBallBoard); _this.world.addContactMaterial(contactMaterialBallBoard);
_this.world.addContactMaterial(contactMaterialBallPlane); _this.world.addContactMaterial(contactMaterialBallPlane);
var ball = new p2.Circle({ radius: 60 }); var circleShape = new p2.Circle({
_this.basketball = new p2.Body({ radius: 1,
material: materialPoint,
});
var circleBody = _this.circleBody = new p2.Body({
mass: 0,
position: [60, 500],
type: p2.Body.STATIC,
});
circleBody.addShape(circleShape);
_this.world.addBody(circleBody);
var ellipse = _this.createBox(0, 0, 20, 20);
circleBody.displays = [ellipse];
var circleShape1 = new p2.Circle({
radius: 1,
material: materialPoint,
});
var circleBody1 = _this.circleBody1 = new p2.Body({
mass: 0, mass: 0,
position: [200, 500],
type: p2.Body.STATIC,
});
circleBody1.addShape(circleShape1);
_this.world.addBody(circleBody1);
var ellipse2 = _this.createBox(0, 0, 20, 20);
circleBody1.displays = [ellipse2];
var ball = new p2.Circle({ radius: 60 });
var basketball = _this.basketball = new p2.Body({
mass: 1,
position: [300, 250], position: [300, 250],
velocity: [-40, 0], velocity: [0, 40],
material: materialBall material: materialBall,
}); });
_this.basketball.addShape(ball); _this.basketball.addShape(ball);
_this.world.addBody(_this.basketball); _this.world.addBody(_this.basketball);
var balldisplay = new engine.Texture(getTextureByName('篮球')); _this.ball = _this.createCircle();
var balldisplays = new engine.Sprite(balldisplay); _this.basketball.displays = [_this.ball];
_this.addChild(balldisplays); var stringMark = _this.stringMark;
_this.basketball.displays = [balldisplays]; var self = _this;
var plane = new p2.Plane(); var plane = new p2.Plane();
_this.planeBody = new p2.Body({ position: [engine.gameStage.width / 2, engine.gameStage.height - 100] }); _this.planeBody = new p2.Body({
position: [engine.gameStage.width, engine.gameStage.height - 200],
material: materialPlane,
});
_this.planeBody.angle = Math.PI; _this.planeBody.angle = Math.PI;
_this.planeBody.addShape(plane); _this.planeBody.addShape(plane);
_this.world.addBody(_this.planeBody); _this.world.addBody(_this.planeBody);
_this.plane = _this.createPlane(); _this.plane = _this.createPlane();
_this.planeBody.displays = [_this.plane]; _this.planeBody.displays = [_this.plane];
_this.world.on('beginContact', function (e) {
if (e.bodyA == circleBody || e.bodyA == circleBody1) {
if (basketball.position[0] > circleBody.position[0] &&
basketball.position[0] < circleBody1.position[0]) {
if (basketball.position[1] > circleBody.position[1] || basketball.position[1] > circleBody1.position[1]) {
console.log("上往下");
self.isRight = !self.isRight;
self.changePosition();
}
}
}
});
_this.addEventListener(engine.Event.ENTER_FRAME, _this.onEnterFrame, _this); _this.addEventListener(engine.Event.ENTER_FRAME, _this.onEnterFrame, _this);
return _this; return _this;
} }
...@@ -194,21 +247,30 @@ ...@@ -194,21 +247,30 @@
var display = body.displays[0]; var display = body.displays[0];
display.x = body.position[0]; display.x = body.position[0];
display.y = body.position[1]; display.y = body.position[1];
display.rotation = body.angle * 180 / Math.PI; display.rotation = (body.angle * 180) / Math.PI;
}
if (this.basketball.position[0] < -120) {
this.basketball.position[0] = 750;
}
if (this.basketball.position[0] > 750 + 120) {
this.basketball.position[0] = -120;
} }
}; };
GameView.prototype.createBox = function () { GameView.prototype.createBox = function (x, y, w, h) {
var sp = new engine.Shape(); var sp = new engine.Shape();
sp.beginFill(0x0000ff); sp.beginFill(0x0000ff);
sp.drawRect(0, 0, 50, 50); sp.drawRect(x, y, w, h);
sp.endFill(); sp.endFill();
sp.anchorX = sp.width / 2;
sp.anchorY = sp.height / 2;
this.addChild(sp);
return sp; return sp;
}; };
GameView.prototype.createPlane = function () { GameView.prototype.createPlane = function () {
var sp = new engine.Shape(); var sp = new engine.Shape();
sp.beginFill(0x0000ff, 1); sp.beginFill(0x0000ff, 1);
sp.moveTo(0, 0); sp.drawRect(0, -10, engine.gameStage.width, 10);
sp.lineTo(engine.gameStage.width, 0); sp.endFill();
sp.anchorX = sp.width / 2; sp.anchorX = sp.width / 2;
sp.anchorY = sp.height / 2; sp.anchorY = sp.height / 2;
this.addChild(sp); this.addChild(sp);
...@@ -217,17 +279,46 @@ ...@@ -217,17 +279,46 @@
GameView.prototype.createCircle = function () { GameView.prototype.createCircle = function () {
var sp = new engine.Shape(); var sp = new engine.Shape();
sp.beginFill(0xff0000, 0.7); sp.beginFill(0xff0000, 0.7);
sp.drawCircle(200, 150, 60); sp.drawCircle(0, 0, 60);
sp.endFill(); sp.endFill();
sp.anchorX = sp.width / 2; sp.anchorX = sp.width / 2;
sp.anchorY = sp.height / 2; sp.anchorY = sp.height / 2;
this.addChild(sp);
return sp; return sp;
}; };
GameView.prototype.createEclipse = function () {
var sp = new engine.Shape();
sp.beginFill(0x00ff00, 0.4);
sp.drawEllipse(0, 0, 200, 80);
sp.endFill();
this.addChild(sp);
return sp;
};
GameView.prototype.changePosition = function () {
this.boxBody.position[0] = 740;
this.boxBody.position[1] = 400;
this.circleBody.position[0] = 540;
this.circleBody.position[1] = 400;
this.circleBody1.position[0] = 700;
this.circleBody1.position[1] = 400;
};
GameView.prototype.onMouseDown = function (e) {
var direction = this.isRight ? -1 : 1;
this.basketball.velocity[0] = this.ballAddSpeedX * direction;
this.basketball.velocity[1] = this.ballAddSpeedY;
this.basketball.angularVelocity = -2 * direction;
};
GameView.prototype.addEvent = function () {
this.addEventListener(engine.Event.ENTER_FRAME, (this.onEnterFrameBind = this.onEnterFrame.bind(this)));
};
GameView.prototype.removeEvent = function () {
this.removeEventListener(engine.Event.ENTER_FRAME, this.onEnterFrameBind);
};
GameView.prototype.start = function () { GameView.prototype.start = function () {
this.stage.addEventListener(engine.MouseEvent.CLICK, this.onMouseDown, this);
console.log(this.world); console.log(this.world);
}; };
GameView.prototype.stop = function () { }; GameView.prototype.stop = function () { };
GameView.prototype.addEvent = function () { };
return GameView; return GameView;
}(engine.Container)); }(engine.Container));
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment