【问题标题】:How can I rotate ( moving ) the Background cocos2d-android?如何旋转(移动)背景 cocos2d-android?
【发布时间】:2013-07-11 13:06:12
【问题描述】:

我有背景,我只想重复我的背景来自:

  1. 从上到下

  2. 从下到上

  3. 从右到左的方向

  4. 从左到右的方向

我该怎么办?

【问题讨论】:

  • 为什么不使用 CCParallaxNode 呢?
  • 从这里,我可以旋转我的背景连续吗?

标签: android cocos2d-iphone cocos2d-x cocos2d-android


【解决方案1】:

使用触摸事件,只需应用 setTranslationX 和 setTranslationY。它将旋转背景图像.. 编写侦听器代码图像对象,如 setontouchlistener 触摸事件方法

 float previousX = 0,previousY = 0;

 switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
             previousX = ev.getX();
             previousY = ev.getY();
            break;
    case MotionEvent.ACTION_MOVE:

    final float deltaX = ev.getX() - previousX;
            final float deltaY = ev.getY() - previousY;
            objectName.setTranslationX(this.getTranslationX() + deltaX);
            objectName.setTranslationY(this.getTranslationY() + deltaY);

            previousX = ev.getX();
            previousY = ev.getY();
            break;
}

【讨论】:

  • 感谢您的回复,但我想在没有任何触摸监听器的情况下旋转背景。就像背景不断旋转
  • 在 android 的 res 文件夹中创建 anim 文件夹。并在该 anim->anim.xml 文件中编写旋转动画代码。并通过主要活动调用它。在给出该图像 xml 代码之前,例如 android:rotation=360 degree..
  • 我在 cocos2d-android 上用这个东西,我还是做不到
【解决方案2】:

没有触摸监听器..它用于 android 中的简单旋转动画...

创建一个新的 Android 项目后,在 res 中创建一个名为 anim 的文件夹,并在 res/anim 中创建一个名为 rotator.xml 的文件。

 <?xml version="1.0" encoding="utf-8"?>
 <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:toYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="4000"
 />

希望代码很容易解释。在这里,一个完整的旋转将在 4000 毫秒(4 秒)内完成。现在将要旋转的 PNG 图像添加到可绘制文件夹中。然后打开res/main.xml,去掉布局中默认的textView后,在布局中添加一个ImageView和Button。将 ImageView 的 src 属性设置为你添加的图片的文件名,例如

android:src="@drawable/myimg"

好的,让我们编辑主类。在按钮的 onClick() 中,添加运行动画所需的代码。检查以下代码。

public class AnimationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener()    {
            @Override
            public void onClick(View arg0) {
                final ImageView myImage = (ImageView)findViewById(R.id.imageView1);
                final Animation
             myRotation =  AnimationUtils.loadAnimation(getApplicationContext(),
     R.anim.rotator);
                myImage.startAnimation(myRotation);
            }
        });
    }
   }

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 2023-03-23
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多