首页 \ 问答 \ 绘制精灵的边界框(drawing bounding box of sprite)

绘制精灵的边界框(drawing bounding box of sprite)

在cocos2d-x中,如何显示作为该层子元素的所有精灵的边界框?

这是我的出发点:

void MyLayer::draw()
{
    // super draw   
    CCLayer::draw();

    // Iterate through all nodes of this layer
    for ( CCNode* node = ??? )
    {
        // Make sure the node is a CCSprite
        if ( node == CCSprite ??? )
        {
            CCSprite* sprite = (CCSprite*) node;
            ccDrawRect( sprite->boundingBox() ??? );
        }

    }
}

In cocos2d-x, how can I display the bounding box for all sprites that are children of this layer?

Here's my starting point:

void MyLayer::draw()
{
    // super draw   
    CCLayer::draw();

    // Iterate through all nodes of this layer
    for ( CCNode* node = ??? )
    {
        // Make sure the node is a CCSprite
        if ( node == CCSprite ??? )
        {
            CCSprite* sprite = (CCSprite*) node;
            ccDrawRect( sprite->boundingBox() ??? );
        }

    }
}

原文:https://stackoverflow.com/questions/12698247
更新时间:2021-09-14 07:09

最满意答案

如果你想使用AJAX来做,那么让你的PHP脚本返回POSTed值并在AJAX请求的成功处理程序中获取它。 为了使成功处理程序能够更改testNumber的值,必须在function otvaranje之外声明该变量。

以下是必要的修改(还要注意我添加的评论):

PHP

if(isset($_POST['broj'])){
    $br = $_POST['broj'];
    $file = fopen($br . "broj.txt", "w");
    fclose($file);

    // Just output the value and exit
    // If there is business logic doing something with
    // $br, then move this after that code.
    // But remember to only do this if the script was 
    // called with POST, otherwise the initial request 
    // wouldn't work anymore.
    print $br;
    exit;
}

JavaScript的

var testNumber = "<?php echo $br; ?>";

function slanje(br){
    $.ajax({
        type: 'POST',
        dataType: 'html',
        data: {
            'broj' : br,
        },
        success: function(data) {
            // called when the request succeeded
            testNumber = data;
        }
    });   
}

function otvaranje(id, broj){
    slanje(broj);
    // move outside: var testNumber = <?php echo $br; ?> + "";
    // rest of function content
}

If you want to do it using AJAX, then let your PHP script return the POSTed value and grab it in the success handler of the AJAX request. For the success handler to be able to change the value of testNumber, the variable has to be declared outside of function otvaranje.

Here are the necessary modifications (also take note of the comments I added):

PHP

if(isset($_POST['broj'])){
    $br = $_POST['broj'];
    $file = fopen($br . "broj.txt", "w");
    fclose($file);

    // Just output the value and exit
    // If there is business logic doing something with
    // $br, then move this after that code.
    // But remember to only do this if the script was 
    // called with POST, otherwise the initial request 
    // wouldn't work anymore.
    print $br;
    exit;
}

JavaScript

var testNumber = "<?php echo $br; ?>";

function slanje(br){
    $.ajax({
        type: 'POST',
        dataType: 'html',
        data: {
            'broj' : br,
        },
        success: function(data) {
            // called when the request succeeded
            testNumber = data;
        }
    });   
}

function otvaranje(id, broj){
    slanje(broj);
    // move outside: var testNumber = <?php echo $br; ?> + "";
    // rest of function content
}

相关问答

更多