Posted By

jeffnehlsen on 02/02/11


Tagged

testing


Versions (?)

Testing


 / Published in: ActionScript 3
 

This is for learning how to use Snipplr on my website.

  1. package lib.actors
  2. {
  3. import Box2D.Collision.Shapes.b2CircleShape;
  4. import Box2D.Common.Math.b2Vec2;
  5. import Box2D.Dynamics.b2Body;
  6. import Box2D.Dynamics.b2BodyDef;
  7. import Box2D.Dynamics.b2FixtureDef;
  8. import flash.display.DisplayObjectContainer;
  9. import flash.display.Sprite;
  10. import flash.events.MouseEvent;
  11. import flash.geom.Point;
  12. import lib.*;
  13. import flash.display.DisplayObject;
  14. /**
  15. * ...
  16. * @author Jeff
  17. */
  18. public class PaddleActor extends Actor
  19. {
  20. private static const DIAMETER:uint = 70;
  21. private var _playerNumber:Boolean;
  22.  
  23. public function PaddleActor(parent:DisplayObjectContainer, playerNum:Boolean)
  24. {
  25. _playerNumber = playerNum;
  26. //_body.SetSleepingAllowed = false;
  27. super(parent, createPaddleBody(), createPaddleCostume(parent));
  28. }
  29.  
  30. private function createPaddleBody():b2Body
  31. {
  32. var location:Point = new Point();
  33. location.x = (_playerNumber) ? 200 : 700;
  34. location.y = 300;
  35.  
  36. // Create shape definition
  37. var circleBodyDef:b2BodyDef = new b2BodyDef();
  38. circleBodyDef.position.Set(location.x / WorldInfo.RATIO, location.y / WorldInfo.RATIO);
  39. //circleBodyDef.linearVelocity.Set(initVel.x / WorldInfo.RATIO, initVel.y / WorldInfo.RATIO);
  40. circleBodyDef.type = b2Body.b2_kinematicBody;
  41. var circleBody:b2Body = WorldInfo.world.CreateBody(circleBodyDef);
  42.  
  43. // Create the circle shape.
  44. var circleShape:b2CircleShape = new b2CircleShape(DIAMETER / 2 / WorldInfo.RATIO);
  45.  
  46. // Create the fixture
  47. var circleFixtureDef:b2FixtureDef = new b2FixtureDef();
  48.  
  49. circleFixtureDef.shape = circleShape;
  50.  
  51. // Assign attributes of ball.
  52. circleFixtureDef.density = 1;
  53. circleFixtureDef.friction = 0;
  54. circleFixtureDef.restitution = 1;
  55.  
  56. // Assign the fixture to the body.
  57. circleBody.CreateFixture(circleFixtureDef);
  58.  
  59. return circleBody;
  60. }
  61.  
  62. private function createPaddleCostume(parent:DisplayObjectContainer):Sprite
  63. {
  64. var paddle:Sprite = (_playerNumber) ? new PaddlePlayer1() : new PaddlePlayer2();
  65. paddle.scaleX = DIAMETER / paddle.width;
  66. paddle.scaleY = DIAMETER / paddle.height;
  67. parent.addChild(paddle);
  68. return paddle;
  69. }
  70.  
  71. protected override function childUpdate():void {
  72. if (_playerNumber) {
  73. // Player 1 stuff
  74. //_body.SetPosition(new b2Vec2(_parent.mouseX / WorldInfo.RATIO, _parent.mouseY / WorldInfo.RATIO));
  75. //_body.SetLinearVelocity(new b2Vec2(_parent.mouseX
  76. //= boxMouseX * _mouseSecondsToReach / _timeStep;
  77. } else {
  78. // Player 2. AI?
  79. }
  80. }
  81.  
  82. public function get playerNumber():Boolean
  83. {
  84. return _playerNumber;
  85. }
  86.  
  87. public function set playerNumber(value:Boolean):void
  88. {
  89. _playerNumber = value;
  90. }
  91. }
  92.  
  93. }

Report this snippet  

You need to login to post a comment.