public void reset() { Random random = new Random(); int localtag = 0; localtag++; // TO TRIGGER THE BUG: // remove the itself from parent from an action // The menu will be removed, but the instance will be alive // and then a new node will be allocated occupying the memory. // => CRASH BOOM BANG CCNode node = getChildByTag(localtag - 1); Debug.WriteLine("Menu: %p", node); removeChild(node, false); // [self removeChildByTag:localtag-1 cleanup:NO]; CCMenuItem item1 = CCMenuItemFont.itemFromString("One", this, menuCallback); Debug.WriteLine("MenuItemFont: %p", item1); CCMenuItem item2 = CCMenuItemFont.itemFromString("Two", this, menuCallback); CCMenu menu = CCMenu.menuWithItems(item1, item2); menu.alignItemsVertically(); float x = random.Next() * 50; float y = random.Next() * 50; menu.position = CCPointExtension.ccpAdd(menu.position, new CCPoint(x, y)); addChild(menu, 0, localtag); //[self check:self]; }
//BOOL initWithTexture(CCTexture2D* aTexture); //virtual void setTexture(CCTexture2D* newTexture); public void move(float delta) { this.position = CCPointExtension.ccpAdd(position, CCPointExtension.ccpMult(m_velocity, delta)); if (position.x > 320 - radius()) { position = new CCPoint(320 - radius(), position.y); m_velocity.x *= -1; } else if (position.x < radius()) { position = new CCPoint(radius(), position.y); m_velocity.x *= -1; } }
public int check_for_error(CCPoint p1, CCPoint p2, CCPoint p3, CCPoint p4, float s, float t) { // the hit point is p3 + t * (p4 - p3); // the hit point also is p1 + s * (p2 - p1); CCPoint p4_p3 = CCPointExtension.ccpSub(p4, p3); CCPoint p4_p3_t = CCPointExtension.ccpMult(p4_p3, t); CCPoint hitPoint1 = CCPointExtension.ccpAdd(p3, p4_p3_t); CCPoint p2_p1 = CCPointExtension.ccpSub(p2, p1); CCPoint p2_p1_s = CCPointExtension.ccpMult(p2_p1, s); CCPoint hitPoint2 = CCPointExtension.ccpAdd(p1, p2_p1_s); // Since float has rounding errors, only check if diff is < 0.05 if ((Math.Abs(hitPoint1.x - hitPoint2.x) > 0.1f) || (Math.Abs(hitPoint1.y - hitPoint2.y) > 0.1f)) { Debug.WriteLine("ERROR: (%f,%f) != (%f,%f)", hitPoint1.x, hitPoint1.y, hitPoint2.x, hitPoint2.y); return(1); } return(0); }