我将如何修改呢? [英] How would I modify this?

查看:63
本文介绍了我将如何修改呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DragonFire SDK开发我的iPad游戏,但是我不确定如何修改一段代码以仅允许某些定向移动.我不确定的代码部分是这样的:

I''m using DragonFire SDK to develop my iPad game, but I''m not sure how to modify a section of code to only allow certain directional movement. The section of code that I''m not sure of is this:

void OnTimer()
{
    int BallX, BallY;
	int AccX, AccY, AccZ;
    // Get tilt (accelerometer) data for each axis.
	AccX = TiltGetx();
	AccY = TiltGety();
	AccZ = TiltGetz();    
    // Update the ball's next X and Y values based on G-Force and the strength of gravity:
    BallX = ViewGetx(BallView) + (AccX / 100);
	BallY = ViewGety(BallView) + (AccY / 100);
    // Don't let the ball go off the screen:
    // Stops for Landscape:
    if (BallY >= 731)
        BallY = 731;
    if (BallY <= 0)
        BallY = 0;
    if (BallX >= 987)
        BallX = 987;
    if (BallX <= 0)
        BallX = 0;   
/*
    // Stops for Portrait:
    if (BallX >= 731)
        BallX = 731;
    if (BallX <= 0)
        BallX = 0;
    if (BallY >= 987)
        BallY = 987;
    if (BallY <= 0)
        BallY = 0;
*/
	
    // Update the ball's position:
    ViewSetxy(BallView, BallX, BallY);
}


问题是我不确定如何更新逻辑以仅允许向左,向右,向上,向下移动.该代码取自DragonFireSDK API文档( http://www.dragonfiresdk.net/help/DragonFireSDKHelp.html [< ^ ]),因此它将向以下方向移动设备倾斜.



这是完整的代码:


The problem is I''m not sure how I would update my logic to only allow Left, Right, Up, Down only movement. The code was taking from the DragonFireSDK API Docs (http://www.dragonfiresdk.net/help/DragonFireSDKHelp.html[^]), so it will move any direction that the device is tilted.



Here''s the entire code:

//====================================================
// App.cpp
//====================================================
#include "DragonFireSDK.h"

int BallImage;
int BallView;

void AppMain()
{
    // Set up an image of a ball to move around the screen:
    BallImage = ImageAdd("Images/Ball.png");
    BallView = ViewAdd(BallImage, 0, 0);

    LandscapeMode();
}

void AppExit()
{
	// Code to be called on Application Close.

	printf("AppExit");
}

void OnTimer()
{
    int BallX, BallY;
	int AccX, AccY, AccZ;
    // Get tilt (accelerometer) data for each axis.
	AccX = TiltGetx();
	AccY = TiltGety();
	AccZ = TiltGetz();    
    // Update the ball's next X and Y values based on G-Force and the strength of gravity:
    BallX = ViewGetx(BallView) + (AccX / 100);
	BallY = ViewGety(BallView) + (AccY / 100);
    // Don't let the ball go off the screen:
    // Stops for Landscape:
    if (BallY >= 731)
        BallY = 731;
    if (BallY <= 0)
        BallY = 0;
    if (BallX >= 987)
        BallX = 987;
    if (BallX <= 0)
        BallX = 0;   
/*
    // Stops for Portrait:
    if (BallX >= 731)
        BallX = 731;
    if (BallX <= 0)
        BallX = 0;
    if (BallY >= 987)
        BallY = 987;
    if (BallY <= 0)
        BallY = 0;
*/
	
    // Update the ball's position:
    ViewSetxy(BallView, BallX, BallY);
}

推荐答案

我不确定我是否完全理解您的问题,但是如果要消除对角线运动,则只需使用X或仅使用Y值,以该值为基础.捕获两个值,然后进行比较,然后选择倾斜度最强的值.然后,您只更新球的位置元素,这样它就可以上下或左右移动,而不是对角移动.
I''m not sure I understand your question completely, but if you want to eliminate diagonal movement then you need to use only the X or only the Y value, based on which is stronger. Capture both values and then compare them and select the one with the strongest tilt component. Then you update only that element of the ball''s position so it will move up and down or left and right but not diagonally.


Richard 一个很好的解决方案.
使用另一种方法,您可以决定仅在倾斜度果断地定向到允许的方向之一时才移动球,而放弃对角线区域"中的运动.
倾斜度的平方(相对于平方)为:

Richard as already suggested a good solution.
Using another approach you may decide to move the ball only if the tilt is decisively oriented into one of the allowed directions, discarding movements in the diagonal ''zone''.
The (squared, for semplicity) relative components of the tilt are:

double rax2 = ((double)AccX*AccX/(AccX*AccX+AccY*AccY);
double ray2 = ((double)AccY*AccY/(AccX*AccX+AccY*AccY);



假设您使用 30°度角进行区分,则:



suppose you use a 30° degrees angle to discriminate, then:

  1. if rax2 >= 0.75然后选择x运动.
  2. 如果ray2 >= 0.75然后选择y运动.
  3. 如果两者同时rax2 < 0.75 ray2 < 0.75,则您不选择移动.
  1. if rax2 >= 0.75 then you choose x movement.
  2. if ray2 >= 0.75 then you choose y movement.
  3. if both rax2 < 0.75 and ray2 < 0.75 then you choose no movement.


这篇关于我将如何修改呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆