Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- "use strict";
- // http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_function
- var throwerLeftX = 0;
- var throwerTopY = 0;
- // imports
- var CommandEnum = com.dgsprb.quick.CommandEnum;
- var Quick = com.dgsprb.quick.Quick;
- var GameObject = com.dgsprb.quick.GameObject;
- var Rect = com.dgsprb.quick.Rect;
- var ImageFactory = com.dgsprb.quick.ImageFactory;
- var Scene = com.dgsprb.quick.Scene;
- var Text = com.dgsprb.quick.Text;
- // static
- function main() {
- Quick.setAutoScale(false);
- Quick.setName("Lies&Cowpies");
- Quick.init(function () { return new FirstScene() });
- }
- var Background = (function () {
- function Background() {
- GameObject.call(this);
- this.setColor("Green");
- this.setWidth(Quick.getWidth());
- this.setHeight(Quick.getHeight());
- };
- Background.prototype = Object.create(GameObject.prototype);
- return Background;
- })();
- var Ball = (function () { // Ball class namespace - this reads just like "class Ball" in Java and encompasses the class definition until the last line of this excerpt
- function Ball() { // Ball class constructor method - what happens when "new Ball()" is issued
- GameObject.call(this); // call the constructor from the superclass - this is done automatically in Java
- this.setImageId("cowpieSprite"); // setImageId is a method inherited from Sprite - correct!
- this.pointer = Quick.getPointer(); // pointer is a member property of Ball
- // getPointer is a static method of Quick, I guess a static method is just a method of Quick, which is a static class whose public members are accessible without creating an instance of that class. - you nailed it!
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Ball
- // this.setEssential(); // a method of GameObject, sets the Ball object as essential to its Scene,
- // I don't what it means to be essential to its Scene - it means that if this object expires,
- // the scene will expire too.
- this.setSolid(); // a method of GameObject , so Ball can collide with other solid objects - correct!
- // this.setCenterX((Quick.getWidth() / 2) - 20);
- this.setBottom(Quick.getHeight() - this.getHeight());
- this.setTop(500);
- this.controller = Quick.getController();
- this.pointer = Quick.getPointer();
- };
- Ball.prototype = Object.create(GameObject.prototype); // this says the Ball class inherits from GameObject - reads just like "extends GameObject" in Java
- // override - this comment means the following method overrides (rewrites) the method with the same name defined in the super class (in this case, GameObject) and though it's not necessary to put that comment there it can be useful for code readers
- Ball.prototype.onCollision = function (gameObject) { // still no instance of Ball here, just class method definition - no instance of this class is created until "new Ball()" is issued
- var collision = this.getCollision(gameObject); // a method of Ball Rect to get direction at collision - right!
- // this.setSize(64, 64);
- // this.bounceFrom(collision); // a method of Ball Rect to bounce away from the current direction - right!
- if (gameObject.hasTag("lies02")) { // a method of GameObject , "returns true if object contains the given tag" - perfect!
- Quick.play("pingSound"); // calls a static method from Quick class
- }
- }; // endof Ball.prototype.onCollision
- // override
- /* Ball.prototype.offBoundary = function (gameObject) {
- var isItTrue = 'true';
- this.expire();
- if(this.getExpired() == isItTrue) {alert("Yes, Ball.prototype.offBoundary IS Expired"); };
- if(this.getExpired() != isItTrue) {alert("No, Ball.prototype.offBoundary IS NOT Expired"); };
- };
- */
- return Ball; // finally publishes the class to the outer scope
- })();
- //
- //
- var Truth = (function () {
- function Truth() {
- GameObject.call(this);
- // this.setImageId("truth02Sprite");
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
- this.setEssential();
- this.setSolid();
- this.setLeft(0);
- this.setTop(100);
- this.setSpeedX(2);
- };
- Truth.prototype = Object.create(GameObject.prototype);
- Truth.prototype.offBoundary = function (gameObject) {
- this.bounceX(); // for the horizontal axis
- this.setImage(ImageFactory.mirror(this.getImage()));
- // alert("Truth.prototype.offBoundary");
- };
- return Truth;
- })();
- //
- var Lies = (function () {
- function Lies() {
- GameObject.call(this);
- // this.setImageId("lies02Sprite");
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
- this.setEssential();
- this.setSolid();
- this.setLeft(0);
- this.setTop(120);
- this.setSpeedX(4);
- };
- Lies.prototype = Object.create(GameObject.prototype);
- Lies.prototype.offBoundary = function (gameObject) {
- this.bounceX(); // for the horizontal axis
- this.setImage(ImageFactory.mirror(this.getImage()));
- // alert("Lies.prototype.offBoundary");
- };
- return Lies;
- })();
- //
- var FirstScene = (function () {
- function FirstScene() {
- Scene.call(this);
- this.add(new Background());
- var ball = new Ball();
- // this.add(ball);
- var thrower = new Thrower();
- this.add(thrower);
- var truth02 = new Truth();
- this.add(truth02);
- truth02.addTag("truth02");
- truth02.setImageId("truth02Sprite");
- truth02.setLeft(0);
- truth02.setTop(120);
- truth02.setSpeedX(2);
- var lies02 = new Lies();
- this.add(lies02);
- lies02.addTag("lies02");
- lies02.setImageId("lies02Sprite");
- lies02.setLeft(0) // (-64);
- lies02.setTop(120);
- lies02.setSpeedX(4);
- // Quick.play("pongSound");
- };
- FirstScene.prototype = Object.create(Scene.prototype);
- // override
- FirstScene.prototype.getNext = function () {
- return new FirstScene();
- };
- return FirstScene;
- })();
- var Thrower = (function () {
- function Thrower() {
- GameObject.call(this);
- this.addTag("thrower");
- this.controller = Quick.getController();
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
- this.pointer = Quick.getPointer();
- this.setImageId("throwerSprite");
- this.setSolid();
- this.setEssential();
- this.setCenterX(Quick.getWidth() / 2);
- this.setBottom(Quick.getHeight() - this.getHeight());
- // this.setX(400);
- this.setTop(500);
- };
- Thrower.prototype = Object.create(GameObject.prototype);
- Thrower.prototype.fire = function () {
- var ball = new Ball(); // create a brand new ball to be thrown
- var scene = this.getScene(); // ask for the scene the avatar is
- scene.add(ball); // add the ball to the same scene I am
- ball.setX(this.getX()); // within your Thrower update method
- // ball.setSpeedX(0);
- ball.setSpeedY(-9);
- // alert('ball.getX = ' + ball.getX() + ' this.getX = ' + this.getX());
- };
- Thrower.prototype.update = function () {
- if (this.controller.keyDown(CommandEnum.LEFT) && this.getLeft() > 0) {
- this.moveX(-8);
- }
- if (this.controller.keyDown(CommandEnum.RIGHT) && this.getRight() < Quick.getWidth()) {
- this.moveX(8);
- }
- if (this.controller.keyDown(CommandEnum.UP) || this.controller.keyDown(CommandEnum.A))
- { // added UP to A (space bar, fire button) because it is a more traditional fire key
- // this.ball.setX(this.getX()); // within your Thrower update method
- this.fire(); // call the method we defined above
- // alert('this.getX = ' + this.getX());
- }
- }
- Thrower.prototype.offBoundary = function (gameObject) {
- this.setCenterX(Quick.getWidth() / 2);
- };
- return Thrower;
- })();
- main();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement