真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Cocos2D-Android-1之源碼詳解:5.Box2dTest

package org.cocos2d.tests;

沈河ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!

import java.util.Iterator;

import org.cocos2d.actions.UpdateCallback;

import org.cocos2d.config.ccMacros;

import org.cocos2d.events.CCTouchDispatcher;

import org.cocos2d.layers.CCLayer;

import org.cocos2d.layers.CCScene;

import org.cocos2d.nodes.CCDirector;

import org.cocos2d.nodes.CCLabel;

import org.cocos2d.nodes.CCSprite;

import org.cocos2d.nodes.CCSpriteSheet;

import org.cocos2d.opengl.CCGLSurfaceView;

import org.cocos2d.types.CGPoint;

import org.cocos2d.types.CGRect;

import org.cocos2d.types.CGSize;

import org.cocos2d.types.ccColor3B;

import android.app.Activity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.Window;

import android.view.WindowManager;

import com.badlogic.gdx.math.Vector2;

import com.badlogic.gdx.physics.box2d.Body;

import com.badlogic.gdx.physics.box2d.BodyDef;

import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import com.badlogic.gdx.physics.box2d.EdgeShape;

import com.badlogic.gdx.physics.box2d.FixtureDef;

import com.badlogic.gdx.physics.box2d.PolygonShape;

import com.badlogic.gdx.physics.box2d.World;

/** 

 * A test that demonstrates basic JBox2D integration by using AtlasSprites connected to physics bodies.

 *

 *

 * This implementation is based on the original Box2DTest (from cocos2d-iphone) but using the JBox2D

 * library and adjusting for differences with the new API as well as some differences in sensitivity

 * (and such) that were observed when testing on the Android platform.

 * 

 * @author Ray Cardillo

 */

// Box2dTest, there is a downloadable demo here:

//   http://code.google.com/p/cocos2d-android-1/downloads/detail?name=cocos2d%20%20and%20jbox2d.3gp&can=2&q=#makechanges

//

public class Box2dTest extends Activity {//物理盒子系統(tǒng)

    // private static final String LOG_TAG = JBox2DTest.class.getSimpleName();

    

static {

        System.loadLibrary("gdx");//加載一個(gè)gdx庫(kù)

}

    private CCGLSurfaceView mGLSurfaceView;//創(chuàng)建一個(gè)view

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);//無(wú)標(biāo)題

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

                WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 

WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//不黑屏

mGLSurfaceView = new CCGLSurfaceView(this);//生成view,并關(guān)聯(lián)上下文到導(dǎo)演

CCDirector director = CCDirector.sharedDirector();//生成得到導(dǎo)演(唯一)

director.attachInView(mGLSurfaceView);//把view交給導(dǎo)演的list

director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);//橫屏

setContentView(mGLSurfaceView);//把view映射刀屏幕

        // show FPS

        CCDirector.sharedDirector().setDisplayFPS(true);//顯示幀頻率

        // frames per second

        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);//幀速

        CCScene scene = CCScene.node();//得到一個(gè)場(chǎng)景

        scene.addChild(new Box2DTestLayer());//把一個(gè)box的圖層添加到場(chǎng)景里

        // Make the Scene active

        CCDirector.sharedDirector().runWithScene(scene);//讓導(dǎo)演運(yùn)行這個(gè)場(chǎng)景,運(yùn)行到剛才的view中

    }

    @Override

    public void onStart() {//開(kāi)始方法

        super.onStart();

    }

    @Override

    public void onPause() {//暫停方法

        super.onPause();

        CCDirector.sharedDirector().onPause();

    }

    @Override

    public void onResume() {

        super.onResume();

        CCDirector.sharedDirector().onResume();

    }

    @Override

    public void onDestroy() {//銷(xiāo)毀方法

        super.onDestroy();

        CCDirector.sharedDirector().end();

        //CCTextureCache.sharedTextureCache().removeAllTextures();

    }

    //

    // Demo of calling integrating Box2D physics engine with cocos2d sprites

    // a cocos2d example

    // http://code.google.com/p/cocos2d-iphone

    //

    // by Steve Oldmeadow

    //

    static class Box2DTestLayer extends CCLayer {//以下是這個(gè)方法生成的方法

        public static final int kTagTileMap = 1;

        public static final int kTagSpriteManager = 1;

        public static final int kTagAnimation1 = 1;

// Pixel to meters ratio. Box2D uses meters as the unit for measurement.

// This ratio defines how many pixels correspond to 1 Box2D "meter"

// Box2D is optimized for objects of 1x1 meter therefore it makes sense

// to define the ratio so that your most common object type is 1x1 meter.

        protected static final float PTM_RATIO = 32.0f;

        

        // Simulation space should be larger than window per Box2D recommendation.

        protected static final float BUFFER = 1.0f;

        

        //FPS for the PhysicsWorld to sync to

        protected static final float FPS = (float)CCDirector.sharedDirector().getAnimationInterval();//得到整個(gè)動(dòng)畫(huà)的幀頻率

        private static float rdelta = 0;

        

        protected final World bxWorld;//生成一個(gè)世界的引用..

        

        public Box2DTestLayer() {//構(gòu)造方法

        super();

       

            this.setIsTouchEnabled(true);//可以點(diǎn)擊

        this.setIsAccelerometerEnabled(true);//啟用設(shè)置加速,加速控制器可以啟動(dòng)

            

        CGSize s = CCDirector.sharedDirector().winSize();//得到屏幕的大小引用

      // Define the gravity vector.

        Vector2 gravity = new Vector2(9.8f, -9.8f);//定義一個(gè)二維向量

        float scaledWidth = s.width/PTM_RATIO;//縮放寬

            float scaledHeight = s.height/PTM_RATIO;//縮放高

//        Vector2 lower = new Vector2(-BUFFER, -BUFFER);//更小

//        Vector2 upper = new Vector2(scaledWidth+BUFFER, scaledHeight+BUFFER);//更大

               

        bxWorld = new World(gravity, true);//新建并設(shè)置這個(gè)世界的重力向量

        bxWorld.setContinuousPhysics(true);//連續(xù)物理可用

    // Define the ground body.

            BodyDef bxGroundBodyDef = new BodyDef();//定義地面身體

            bxGroundBodyDef.position.set(0.0f, 0.0f);//身體的位置

   

    // Call the body factory which allocates memory for the ground body

    // from a pool and creates the ground box shape (also from a pool).

    // The body is also added to the world.

            Body groundBody = bxWorld.createBody(bxGroundBodyDef);//將身體添加到世界

         // Define the ground box shape.

            EdgeShape groundBox = new EdgeShape();//定義一個(gè)形狀

            Vector2 bottomLeft = new Vector2(0f,0f);//定義4個(gè)2維向量

            Vector2 topLeft = new Vector2(0f,scaledHeight);

            Vector2 topRight = new Vector2(scaledWidth,scaledHeight);

            Vector2 bottomRight = new Vector2(scaledWidth,0f);

            

    // bottom

    groundBox.set( bottomLeft, bottomRight );//設(shè)置一條線

    groundBody.createFixture(groundBox,0);//把這條線作為物理盒子的邊界

   

    // top

    groundBox.set( topLeft, topRight );//同理

    groundBody.createFixture(groundBox,0);

   

    // left

    groundBox.set( topLeft, bottomLeft );

    groundBody.createFixture(groundBox,0);

   

    // right

    groundBox.set( topRight, bottomRight );

    groundBody.createFixture(groundBox,0);

                                    

            //Set up sprite

             CCSpriteSheet mgr = CCSpriteSheet.spriteSheet("blocks.png", 150);

//建立一個(gè)圖像表單,用來(lái)拆分出小塊

            addChild(mgr, 0, kTagSpriteManager);//表單添加進(jìn)去,順序0,把標(biāo)簽號(hào)定為1

   

            addNewSpriteWithCoords(CGPoint.ccp(s.width / 2.0f, s.height / 2.0f));

            //上面是一個(gè)方法下面解釋

            CCLabel label = CCLabel.makeLabel("Tap screen", "DroidSans", 32);//創(chuàng)建一個(gè)標(biāo)記

            label.setPosition(CGPoint.make(s.width / 2f, s.height - 50f));//設(shè)置坐標(biāo)

            label.setColor(new ccColor3B(0, 0, 255));//設(shè)置顏色

            addChild(label);

        }

        private UpdateCallback tickCallback = new UpdateCallback() {//創(chuàng)建一個(gè)時(shí)間返回

@Override

public void update(float d) {//時(shí)間更新

tick(d);

}

};

        

@Override

public void onEnter() {

super.onEnter();

// start ticking (for physics simulation)

schedule(tickCallback);

}

@Override

public void onExit() {

super.onExit();

// stop ticking (for physics simulation)

unschedule(tickCallback);

}

private void addNewSpriteWithCoords(CGPoint pos) {

      CCSpriteSheet sheet = (CCSpriteSheet) getChildByTag(kTagSpriteManager);//得到一個(gè)表格

    //We have a 64x64 sprite sheet with 4 different 32x32 p_w_picpaths.  The following code is

    //just randomly picking one of the p_w_picpaths

    int idx = (ccMacros.CCRANDOM_0_1() > .5 ? 0:1);//定義一個(gè)隨機(jī)數(shù)。得到1/0

    int idy = (ccMacros.CCRANDOM_0_1() > .5 ? 0:1);

   

//     CCSprite sprite = CCSprite.sprite("blocks.png", CGRect.make(32 * idx,32 * idy,32,32));//生成一個(gè)精靈。用那個(gè)圖片,截取公式位置的圖像

//     this.addChild(sprite);//添加精靈

    CCSprite sprite = CCSprite.sprite(sheet, CGRect.make(32 * idx,32 * idy,32,32));//生成精靈,用剛才的那個(gè)圖集,截取某塊

    sheet.addChild(sprite);//添加子類(lèi)

   

    sprite.setPosition(pos);  //設(shè)置點(diǎn)  

    // Define the dynamic body.

    //Set up a 1m squared box in the physics world

    BodyDef bodyDef = new BodyDef();//新建一個(gè)剛體

    bodyDef.type = BodyType.DynamicBody;//設(shè)置為類(lèi)型3動(dòng)態(tài)剛體

    bodyDef.position.set(pos.x/PTM_RATIO, pos.y/PTM_RATIO);//設(shè)置身體位置

   

    // Define another box shape for our dynamic body.

    PolygonShape dynamicBox = new PolygonShape();//新建多邊形

    dynamicBox.setAsBox(.5f, .5f);//These are mid points for our 1m box

//作為一個(gè)盒子時(shí)的頂點(diǎn)0.5,0.5

//     dynamicBox.density = 1.0f;

//            dynamicBox.friction = 0.3f;

   

        synchronized (bxWorld) {//線程鎖

        // Define the dynamic body fixture and set mass so it's dynamic.

        Body body = bxWorld.createBody(bodyDef);//在世界內(nèi)創(chuàng)建這個(gè)剛體

        body.setUserData(sprite);//使用這個(gè)數(shù)據(jù)精靈

       

        FixtureDef fixtureDef = new FixtureDef();//固定的東西

        fixtureDef.shape = dynamicBox;

        fixtureDef.density = 1.0f;//密度

        fixtureDef.friction = 0.3f;//摩擦系數(shù)

        body.createFixture(fixtureDef);//把這些固定參數(shù)給這個(gè)物體

        }        

        }

        public synchronized void tick(float delta) {//時(shí)間類(lèi)

        if ((rdelta += delta) < FPS) return;//計(jì)算得不用快過(guò)幀..

       

        // It is recommended that a fixed time step is used with Box2D for stability

        // of the simulation, however, we are using a variable time step here.

        // You need to make an informed choice, the following URL is useful

        // http://gafferongames.com/game-physics/fix-your-timestep/

       

        // Instruct the world to perform a simulation step. It is

        // generally best to keep the time step and iterations fixed.

        synchronized (bxWorld) {

        bxWorld.step(FPS, 8, 1);//計(jì)算的速度

        }

       

        rdelta = 0;//累計(jì)時(shí)間

       

        // Iterate over the bodies in the physics world

        Iterator it = bxWorld.getBodies();//新建迭代器得到世界的剛體集合

        while(it.hasNext()) {

        Body b = it.next();//得到剛體

        Object userData = b.getUserData();//剛體的數(shù)據(jù)

       

        if (userData != null && userData instanceof CCSprite) {

//如果數(shù)據(jù)不為空,且是個(gè)精靈的實(shí)例而

        //Synchronize the Sprites position and rotation with the corresponding body

        final CCSprite sprite = (CCSprite)userData;//得到這個(gè)圖像

        final Vector2 pos = b.getPosition();//得到這個(gè)剛體的點(diǎn)

        sprite.setPosition(pos.x * PTM_RATIO, pos.y * PTM_RATIO);

//設(shè)置點(diǎn)

        sprite.setRotation(-1.0f * ccMacros.CC_RADIANS_TO_DEGREES(b.getAngle()));//設(shè)置弧度

        }

        }

        }

        @Override

        public boolean ccTouchesBegan(MotionEvent event) {//觸屏事件

            CGPoint location = CCDirector.sharedDirector()

            .convertToGL(CGPoint.make(event.getX(), event.getY()));//得到點(diǎn)

            addNewSpriteWithCoords(location);//添加一個(gè)物品在那個(gè)點(diǎn)

 

            return CCTouchDispatcher.kEventHandled;//返回?cái)?shù)據(jù)

        }

        static float prevX=0, prevY=0;

        

        Vector2 gravity = new Vector2();//定義2維數(shù)組

        

@Override

   public void ccAccelerometerChanged(float accelX, float accelY, float accelZ) {//當(dāng)加速傳感器有感覺(jué)了

//#define kFilterFactor 0.05f

float kFilterFactor  = 1.0f;// don't use filter. the code is here just as an example

float accX = (float) accelX * kFilterFactor + (1- kFilterFactor)* prevX;//x方向

float accY = (float) accelY * kFilterFactor + (1- kFilterFactor)* prevY;//y方向

prevX = accX;

prevY = accY;

// no filtering being done in this demo (just magnify the gravity a bit)

gravity.set( accY * 9.8f, accX * -9.8f );//得到重力的向量

bxWorld.setGravity( gravity ); //給世界設(shè)置重力向量

}

    }

}


本文標(biāo)題:Cocos2D-Android-1之源碼詳解:5.Box2dTest
標(biāo)題路徑:http://weahome.cn/article/jscjce.html

其他資訊

在線咨詢(xún)

微信咨詢(xún)

電話咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部