void WelcomeScene::ccTouchesEnded(CCSet* touches,CCEvent* event){ CCTouch* touch = (CCTouch*)touches->anyObject(); CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); if(touchId != -1){ for(int i = 0;i < itemsArray->count();i++){ ShopItem* item = (ShopItem*)itemsArray->objectAtIndex(i); if(item->isTouch(location.x - scroll->getPositionX() - scroll->getContentOffset().x, location.y - scroll->getPositionY())){ if(temp != NULL){ temp->setSelected(false); } if(item->getId() == touchId){ if(item->touchAction() == 0){ createItems(item->getType(),scroll->getContentOffset().x); temp = NULL; }else{ temp = item; char t[100]; if(item->getValue() < 10){ sprintf(t,"购买物品需要%d元人民币,点击右侧的买入按钮即可购买",item->getValue()); }else{ sprintf(t,"购买物品需要%d仙桃,点击右侧的买入按钮即可购买",item->getValue()); } tipText->setString(conv(t)); temp->setSelected(true); } } break; } } } touchId = -1; }
ShopItem *ShopItems::findItem(int id) { ShopItem *item; std::vector<ShopItem*>::iterator it; for(it = mShopItems.begin(); it != mShopItems.end(); it++) { item = *(it); if (item->getId() == id) { return item; } } return 0; }
void WelcomeScene::ccTouchesBegan(CCSet* touches,CCEvent* event){ CCTouch* touch = (CCTouch*)touches->anyObject(); CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); for(int i = 0;i < itemsArray->count();i++){ ShopItem* item = (ShopItem*)itemsArray->objectAtIndex(i); if(item->isTouch(location.x - scroll->getPositionX() - scroll->getContentOffset().x, location.y - scroll->getPositionY())){ /*if(item->touchAction() == 0){ createItems(item->getType()); }*/ touchId = item->getId(); lastPt = ccp(location.x,location.y); break; } } }
void SellDialog::action(const gcn::ActionEvent &event) { if (event.getId() == "quit") { close(); return; } int selectedItem = mShopItemList->getSelected(); // The following actions require a valid item selection if (selectedItem == -1 || selectedItem >= (int) mShopItems->getNumberOfElements()) { return; } if (event.getId() == "slider") { mAmountItems = (int) mSlider->getValue(); updateButtonsAndLabels(); } else if (event.getId() == "inc" && mAmountItems < mMaxItems) { mAmountItems++; mSlider->setValue(mAmountItems); updateButtonsAndLabels(); } else if (event.getId() == "dec" && mAmountItems > 1) { mAmountItems--; mSlider->setValue(mAmountItems); updateButtonsAndLabels(); } else if (event.getId() == "max") { mAmountItems = mMaxItems; mSlider->setValue(mAmountItems); updateButtonsAndLabels(); } else if (event.getId() == "sell" && mAmountItems > 0 && mAmountItems <= mMaxItems) { // Attempt sell ShopItem *item = mShopItems->at(selectedItem); int sellCount, itemIndex; mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice(); mMaxItems -= mAmountItems; while (mAmountItems > 0) { // This order is important, item->getCurrentInvIndex() would return // the inventory index of the next Duplicate otherwise. itemIndex = item->getCurrentInvIndex(); sellCount = item->sellCurrentDuplicate(mAmountItems); // For Manaserv, the Item id is to be given as index. if ((Net::getNetworkType() == ServerInfo::MANASERV)) itemIndex = item->getId(); Net::getNpcHandler()->sellItem(mNpcId, itemIndex, sellCount); mAmountItems -= sellCount; } mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice(); mAmountItems = 1; mSlider->setValue(0); if (!mMaxItems) { // All were sold mShopItemList->setSelected(-1); delete mShopItems->at(selectedItem); mShopItems->erase(selectedItem); gcn::Rectangle scroll; scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); scroll.height = mShopItemList->getRowHeight(); mShopItemList->showPart(scroll); } else { mSlider->gcn::Slider::setScale(1, mMaxItems); // Update only when there are items left, the entry doesn't exist // otherwise and can't be updated updateButtonsAndLabels(); } } }