Creating a menu in Cocos2d-Android - cocos2d-android

I am a beginner in Android Development and have been working on a game in cocos2d-android and need to implement a menu screen. I have looked on google and can only find information in regard to cocos2d for iPhone. Would anyone have any advice/ideas as to how I could add a menu, or even better know of any tutorials or books that could help me with this?
Thanks in advance!

Hi you can create menu in cocos2d with use of CCMenu and CCMenuItemLabel here i am sharing you an example from which i made menu screen for option so it will helpful to you here the method which shows you to use of CCMenu and CCMenuItemLabel so try it all the best.
private CCNode addOtherOptions(String strName, int xpos, int ypos, String action) {
CCNode ccStartNode = CCNode.node();
ccStartNode.setPosition(CGPoint.ccp(xpos, ypos));
ccStartNode.setAnchorPoint(CGPoint.ccp(0, 0));
CCMenuItemLabel startGame = CCMenuItemLabel.item(strName, this, action);
startGame.setPosition(CGPoint.ccp(0, 0));
startGame.setAnchorPoint(CGPoint.ccp(0, 0));
startGame.setColor(ccColor3B.ccc3(139, 69, 19));
startGame.setScale(0.5f);
CCMenu menu2 = CCMenu.menu(startGame);
menu2.alignItemsVertically(-10f);
menu2.setPosition(CGPoint.ccp(-20, 0));
menu2.setAnchorPoint(CGPoint.ccp(0, 0));
// addChild(menu2);
ccStartNode.addChild(menu2);
return ccStartNode;
}

Related

Change border color of QWidget using animation

I have tried to change border color of Qwidget for two days but Doesn't work.
so I refered to another code which is posted on the stackoverflow about moving widget. it worked perpectly but I can't change border color using QPropertyAnimation. give some tips for this problem.
QPropertyAnimation *animation = new QPropertyAnimation(ui.defectView, "border-color");
animation->setDuration(2000);
animation->setStartValue(QColor(0, 0, 0));
animation->setEndValue(QColor(240, 240, 240));
animation->start();
There is no QWidget property called "border-color". All it's properties are shown in QWidget documentation. But you can change this color using QSS and animate it. Also consider custom paintEvent depending on your needs.

How can i move the QWidget in QGraphicsView?

I want to move the QPushButton or kind of QWidget in QGraphicsView for example QListWidet, QTableWidget etc.
So, I used QGraphicsProxyWidget.
Then I clicked the widget and drag. But It is not moving.
How can i move QWidget?
This is my code.
ui->graphicsView->setGeometry(0,0,geometry().width(),geometry().height());
scene = new QGraphicsScene(0, 0, geometry().width(), geometry().height(), this);
ui->graphicsView->setScene(scene);
btn =new QPushButton(NULL);
QGraphicsProxyWidget *proxy = scene->addWidget(btn);
proxy->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable);
scene->addItem(proxy);
Can you tell me What is wrong?
Thanks.
Cause
In your case you cannot move the button, because although you have made the proxy movable, the button handles the mouse input differently in order to implement its clicked functionality. In other words, on a mouse press, how could a move action be differentiated from a click action?
Solution
My solution would be to make the button part of a somewhat bigger QGraphicsRectItem. The latter will serve as a draggable handle, i.e. if the user interacts with the button - it clicks, but if the other portion of the QGraphicsRectItem (not covered by the button) is interacted with, the button could be moved around.
Example
Here is an example showing exactly how to do that:
auto *item = new QGraphicsRectItem(0, 0, 75, 40);
auto *proxy = new QGraphicsProxyWidget(item);
auto *btn = new QPushButton(tr("Click me"));
btn->setGeometry(0, 0, 77, 26);
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setBrush(Qt::darkYellow);
proxy->setWidget(btn);
proxy->setPos(0, 15);
proxy->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable);
scene->addItem(item);
Note: To make this work it is important to set the parent of proxy to item.

Cocos2d CCMenuItem not responding

I'm having some trouble with have a CCMenuItem button work. I've followed all instructions/advice given in previous examples and questions, but nothing seems to work. Whenever I click the button, the image doesn't even change to the new image.
CCMenuItem *startButton = [CCMenuItemImage itemWithNormalImage:#"Start Button.png" selectedImage:#"Start Button Selected.png" target:self selector:#selector(startGame:)];
startButton.position = ccp(0, 0);
CCMenu *menu = [CCMenu menuWithItems:startButton, nil];
menu.position = ccp(winSize.width/2, winSize.height/6);
[self addChild:menu];
menu.touchEnabled = YES;
you probably want to use the
menu.enabled=YES;
that property will control whether touch events are processed. The property you are using (touchEnabled) is a basic property of the CCLayer object (CCMenu extends CCLayer), and that controls whether the layer will receive touch events or not.
Although, the 'enabled' property is set to YES during initialization (its default state). It is possible that by using the other method, you are altering the propagation of touch events. Try first to comment out the line.

How to replace scenes when clicked on imageview in cocos2d-android

i have started to develope an game app using cocos2d-android game engine, til now had put 4 imageview and a background image to the scene1, now if clicked on 1st imageview it should navigate to the next scene, i googled and youtube for this but did not succeeded, and also there is no more sources for cocos2d android game engine.
Make your image as menuItem and change your scene on the click method of that menuItem as below
CCMenuItem menuItem1 = CCMenuItemImage.item("backbtn.png", "backbtn.png", this,
"onClickMenuItem");
then handle the onClick method of this menuItem as below
public void onClickMenuItem(Object menuItem) {
if (menuItem == menuItem1) {
CCDirector.sharedDirector().popScene();
}
}
Use CCMenu and CCMenuItemImage or CCMenuItemSprite. You can set method that will be called on menu item click.

Is there a Button such as UIButton in COCOS2D?

I try to search a button such as UIButton in cocos2d..
UIButton can press several buttons at same time.
but CCmenuitem can't..
Is there anyclass in cocos2d such as uibutton?
Extend CCMenu to support multitouch. It was designed to use targeted touches, that is, single touch interactions. With a little elbow grease, it wouldn't be difficult at all to extend it to support multitouch.
CCMenuItems are used instead of UIButton in cocos2d. A single CCMenuItem inside your CCMenu can exactly work like the UIButton.
You can create that in the following way.
//inside your .m file
-(id) init
{
if( (self=[super init] )) {
CCMenuItem *yourMenuItem = [CCMenuItemImage itemFromNormalImage:#"normalImage.png" selectedImage:#"selectedImage.png" target:self selector:#selector(menuItemPressed:)];
yourMenuItem.position = ccp(60, 60);
CCMenu *yourMenu = [CCMenu menuWithItems:yourMenuItem, nil];
yourMenu.position = CGPointZero;
[self addChild:yourMenu];
}
}
There's a good article by Ray Wenderlich on this here.
However, if your main requirement is to add a UIButton only then this discussion might be helpful.
I would also take a look at SneakyInput if you are having trouble setting up your own buttons; it's very easy to use.