无法在spritekit中更新spriteNode位置?

时间:2016-01-21 15:39:39

标签: ios objective-c sprite-kit

基本上当用户点击屏幕时,他们可以在屏幕上添加一个精灵,该精灵包含在一个名为Planet的包装类中,该类包含更多变量。我将所有行星存储在NSMutableArray中,因此当点击屏幕时会创建一个新行星,并将该行星的spriteNode作为子项添加到视图中。我希望能够更新精灵的大小和位置等值,但问题是我似乎无法通过从行星NSMutableArray中的行星类访问精灵来做到这一点

基本上当点击屏幕时,会添加一个行星,直到水龙头结束,调用“更新质量”方法,稍微增加精灵的大小。

<script src="http://code.jquery.com/jquery-1.11.3.js"></script>

<form action="#" method="POST" id="cart_form">
    <table class='header_tbl' style="background:none !important;">

        <thead>
        <tr>
            <th>Item Name</th>
            <th>Unit Price</th>
            <th>Qty</th>
            <th>Amount(RM)</th>
            <th></th>
        </tr>
        </thead>

        <tbody>
        </tbody>
        <tfoot>

        <tr>
            <td>Total</td><td></td><td><span class='qty_1'></span><input type='hidden' name='total_qty' value=''></td><td><span class='total'></span><input type='hidden' name='total' value=''></td><td></td>
        </tr>
        <!--membership-->
        <tr id="membership" class="no_show">
            <td><span class="mem_1"></span></td><td></td><td><span class="mem_2"></span></td><td><span class="mem_3"></span><input type='hidden' name='mem' value='10'><br/><input type='hidden' name='email_2' value=''><input type='hidden' name='ic_add' value=''></td><td></td>
        </tr>

        <tr id="comm" class="no_show">
            <td>Commision (x%)</td><td></td><td></td><td><span class='comm'><?php echo $discount;?></span>%<input type='hidden' name='comm' value='<?php echo $discount;?>'></td><td></td>
        </tr>
        <tr class="no_show">
            <td>Total Payment</td><td></td><td></td><td><span class='grand'></span><input type='hidden' name='grand' value=''></td><td></td>
        </tr>
        <tr id='add_here'>
        </tr>
        <!--cust info starts-->
        <tr class="no_show">
            <td colspan="3"><input type='hidden' name='cust_id' id='cust_id' value=''><br/><input type='hidden' name='cust_email' id='cust_email' value=''></td><td></td>
        </tr>
        <tr class="no_show">
            <td colspan="3"><input type='hidden' name='cust_name' id='cust_name' value=''></td><td></td>
        </tr>
        <!--membership-->
        <tr>
            <td colspan="4"><input type='hidden' name='mem_id' value='<?php echo $membership;?>'></td><td></td>
        </tr>
        <tr>
            <td colspan="4"><input type='hidden' name='mem_name' value='<?php echo $username;?>'></td><td></td>
        </tr>
        <!--membership ends-->
        <!--cust info ends-->
        <!--membership ends-->
        <tr id="replace_this">
            <td></td>
            <td class="cancel"><input type="button" name='cancel' id="cancel" value="CANCEL"/></td>
            <td class="cash"><input type="submit" name='submit' id="cash" value="CASH"/></td>
            <td class="checkout"><input type="button" name='checkout' id="checkout" value="CHECK OUT"/></td>
            <td></td>
        </tr>
        </tfoot>



    </table>
</form>
<div class='small-12 medium-6 large-6 columns' id='left_col'>
    <div class="container">
-----------NEW FORM-----------------
        <form action="#" method="POST" id="cart_form2"> </form>
-----------END NEW FORM-----------------
    </div>
</div>

<button id="btn">Click Me</button>

带有图像类的行星只是创建一个带有几个变量的新行星类,并在其中使用一个spriteNode对象,该对象使用被调用的图像,在本例中为@“Planet”。

1 个答案:

答案 0 :(得分:0)

我会这样做。您是否考虑将currentPlanet保存在实例变量中,对其执行操作,并在touchesEnded:withEvent:中取消该操作?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    mass = 10;
    // grab only the first touch
    self.currentTouch = touches.firstObject;
    CGPoint location = [self.currentTouch locationInNode:self];

    self.currentPlanet = [[Planet alloc] init];
    [self.currentPlanet planetWithImage:@"Earth"];

    self.currentPlanet.mass = mass;
    self.currentPlanet.spriteNode.xScale = self.currentPlanet.spriteNode.yScale = self.currentPlanet.mass * .004;
    self.currentPlanet.spriteNode.position = location;

    SKAction *spinAction = [SKAction rotateByAngle:M_PI_2 duration:5];
    [self.currentPlanet.spriteNode runAction:[SKAction repeatActionForever:spinAction]];
    SKAction *growAction = [SKAction sequence:@[[SKAction waitForDuration:0.2], [SKAction performSelector:@selector(grow) onTarget:self.currentPlanet]];
    [self.currentPlanet.spriteNode runAction:[SKAction repeatActionForever:growAction] withKey:@"growAction"];

    [self addChild:self.currentPlanet.spriteNode];
    [planets addObject:self.currentPlanet];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches containsObject:self.currentTouch]) {
        [self.currentPlanet removeActionForKey:@"growAction"];
    }
}

然后在你的Planet课程中:

@implementation Planet () 
- (void)grow
{
    self.mass++;
    self.spriteNode.xScale = self.spriteNode.yScale = 0.002 * self.mass;
}
@end

这样你就可以直接访问你关心的精灵,而不是试图在行星数组中找到它。如果这是你对行星阵列的唯一用途,那么如果你这样做就可以摆脱它。