void AddBall()
        {
            int idx    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
            int idy    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
            var sprite = new CCPhysicsSprite(ballTexture, new CCRect(32 * idx, 32 * idy, 32, 32), PTM_RATIO);

            ballsBatch.AddChild(sprite);

            CCPoint p = GetRandomPosition(sprite.ContentSize);

            sprite.Position = new CCPoint(p.X, p.Y);
            var def = new b2BodyDef();

            def.position       = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO);
            def.linearVelocity = new b2Vec2(0.0f, -1.0f);
            def.type           = b2BodyType.b2_dynamicBody;
            b2Body body = world.CreateBody(def);

            var circle = new b2CircleShape();

            circle.Radius = 0.5f;

            var fd = new b2FixtureDef();

            fd.shape       = circle;
            fd.density     = 1f;
            fd.restitution = 0.85f;
            fd.friction    = 0f;
            body.CreateFixture(fd);

            sprite.PhysicsBody = body;

            Console.WriteLine("sprite batch node count = {0}", ballsBatch.ChildrenCount);
        }
Example #2
0
        void ShootBullet(CCSprite enemySprite)
        {
            if (ballsBatch.ChildrenCount < MAX_NUM_BALLS)
            {
                int idx    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
                int idy    = (CCRandom.Float_0_1() > .5 ? 0 : 1);
                var sprite = new CCPhysicsSprite(ballTexture, new CCRect(32 * idx, 32 * idy, 32, 32), PTM_RATIO);

                ballsBatch.AddChild(sprite);

                //CCPoint p = GetRandomPosition (sprite.ContentSize);

                sprite.Position = enemySprite.Position;                // new CCPoint (p.X, p.Y);

                var def = new b2BodyDef();
                def.position = new b2Vec2(sprite.Position.X / PTM_RATIO, sprite.Position.Y / PTM_RATIO);
                Random rand = new Random();

                /*if(rand.Next (100) < 50)
                 * def.linearVelocity = new b2Vec2 (-10.0f, 0.0f);
                 * else
                 *      def.linearVelocity = new b2Vec2 (10.0f, 0.0f);*/
                def.linearVelocity = new b2Vec2(0.0f, -1.0f);
                def.type           = b2BodyType.b2_dynamicBody;
                b2Body body = world.CreateBody(def);

                var circle = new b2CircleShape();
                circle.Radius = 0.5f;

                var fd = new b2FixtureDef();
                fd.shape       = circle;
                fd.density     = 1f;
                fd.restitution = 0.85f;
                fd.friction    = 0f;
                body.CreateFixture(fd);

                sprite.PhysicsBody = body;
            }
        }
        void AddBall()
        {
            var contentScale = contentScaleFactorX < contentScaleFactorY ? contentScaleFactorX : contentScaleFactorY;             //use the smaller scale if they are not equal

            if (ballsBatch.ChildrenCount < MAX_NUM_BALLS)
            {
                int idx        = (CCRandom.Float_0_1() > .5 ? 0 : 1);
                int idy        = (CCRandom.Float_0_1() > .5 ? 0 : 1);
                var ballSprite = new CCPhysicsSprite(ballTexture, new CCRect(32 * idx, 32 * idy, 32, 32), PTM_RATIO);
                ballSprite.Scale = contentScale;

                ballsBatch.AddChild(ballSprite);

                CCPoint p = GetRandomPosition(ballSprite.ContentSize);

                ballSprite.Position = new CCPoint(p.X, p.Y);

                var def = new b2BodyDef();
                def.position       = new b2Vec2(p.X / PTM_RATIO, p.Y / PTM_RATIO);
                def.linearVelocity = new b2Vec2(0.0f, -1.0f);
                def.type           = b2BodyType.b2_dynamicBody;
                b2Body body = world.CreateBody(def);

                var circle = new b2CircleShape();
                circle.Radius = 0.5f * contentScale;

                var fd = new b2FixtureDef();
                fd.shape       = circle;
                fd.density     = 1f;
                fd.restitution = 0.85f;
                fd.friction    = 0f;
                body.CreateFixture(fd);

                ballSprite.PhysicsBody = body;
            }
        }
Example #4
0
        void AddBall()
        {
            if (ballsBatch.ChildrenCount < MAX_NUM_BALLS) {
                int idx = (CCRandom.Float_0_1 () > .5 ? 0 : 1);
                int idy = (CCRandom.Float_0_1 () > .5 ? 0 : 1);
                var sprite = new CCPhysicsSprite (ballTexture, new CCRect (32 * idx, 32 * idy, 32, 32), PTM_RATIO);

                ballsBatch.AddChild (sprite);

                CCPoint p = GetRandomPosition (sprite.ContentSize);

                sprite.Position = new CCPoint (p.X, p.Y);

                var def = new b2BodyDef ();
                def.position = new b2Vec2 (p.X / PTM_RATIO, p.Y / PTM_RATIO);
                def.linearVelocity = new b2Vec2 (0.0f, -1.0f);
                def.type = b2BodyType.b2_dynamicBody;
                b2Body body = world.CreateBody (def);

                var circle = new b2CircleShape ();
                circle.Radius = 0.5f;

                var fd = new b2FixtureDef ();
                fd.shape = circle;
                fd.density = 1f;
                fd.restitution = 0.85f;
                fd.friction = 0f;
                body.CreateFixture (fd);

                sprite.PhysicsBody = body;
            }
        }