How to add glowing border effects to a MenuItemImage in coocs2d? - cocos2d-x

I'm new to cocos2d and I'm trying to make a menu. I created a MenuItemImage here:
mSetting = MenuItemImage::create("setting.png", "setting_selected.png");
Now I'm looking for a way to add glowing effect to the border of it in stead of a boring static setting_selected.png image.
(for example: the light-blue glowing border of the menu image here , check at 1:00 https://youtu.be/PJSlvhDbB4I?t=1m).
Your attention and help is very much appreciated :D
Update: I have an idea that using a sprite with square shape, which exactly fits the menu item to make it looks like the border of it. Then make it animate (changing color) to show glowing effects likes how they do in the video. Is that possible, easy and effective to do the job?

You can use MenuItemSprite instead of MenuItemImage and run actions when menu item is clicked. Look at this link:
Cocos2d-x: simple button effect
You should find more actions here: Cocos2d-x actions
I update and write the example to be similar to what you need (It's not exactly what you want but may help you).
I have a scene named EditorScene.
In EditorScene.h:
....
virtual void onMenuItemSelected(Ref *item );
MenuItemSprite* createMenuButton( const char* img );
MenuItemSprite* btEnter;
void actionEnter();
....
And in EditorScene.cpp:
bool EditorScene::init()
{
...
btEnter = createMenuButton("button_normal.png");
auto menu = Menu::create(btEnter, nullptr);
menu->setPosition( 400,400);
addChild(menu);
}
MenuItemSprite* EditorScene::createMenuButton( const char* img )
{
Sprite* spr = Sprite::create( img );
auto light = Sprite::create( "a2.png" );
light->setTag( 1 );
light->setPosition( 0,
spr->getBoundingBox().size.height );
light->setOpacity( 0 );
if( ! spr )
{
return NULL;
}
MenuItemSprite* item = MenuItemSprite::create( spr,
NULL, NULL,
CC_CALLBACK_1( EditorScene::onMenuItemSelected, this ) );
item->addChild(light);
return item;
}
void EditorScene::onMenuItemSelected( Ref* item ) {
Sprite* btm = (Sprite*) item;
Sprite* light = (Sprite*)btm->getChildByTag( 1 );
if( item == btEnter ){
float w = btm->getBoundingBox().size.width;
float h = btm->getBoundingBox().size.height;
std::function<void(void)> f1 = std::bind(&EditorScene::actionEnter, this);
light->runAction(
Sequence::create(
FadeIn::create(0),
MoveBy::create( 0.09f, Vec2(w, 0) ),
MoveBy::create( 0.045f, Vec2(0, -h) ),
MoveBy::create( 0.09f, Vec2(-w, 0) ),
MoveBy::create( 0.045f, Vec2(0, h) ),
FadeOut::create(0),
CallFunc::create( f1 ),
NULL ) );
}
}
void EditorScene::actionEnter()
{
log("Do something!");
}
This code creates a menu item that by clicking shows a small dot orbiting on border of button:
You can use Animation::createWithSpriteFrames if you want to animate your menu item's background.

Related

How to set dynamic text to a variable?

Im trying out adobe animate for school and until now ive been following a cool rpg game tutorial. but the tutorial ended quicky and now im left on my own browsing the internet. i wanted to add a little star collector feature but the tutorial i found is for flash and i dont know how to use it.
their code is pretty much
money = 0;
onClipEvent (enterFrame) {
if (_root.move_mc.hitTest (this)) {
_root.money++;
this._x = -50;
this._y = -50;
}
}
and i hanged it to
var star:Number = 0;
if(linkMc.hitBoxMc.hitTestObject(overworldMc.starMc1))
{
star += 1;
overworldMc.starMc1.alpha = 0;
}
and it works but now i need to figure out a way to set up a text n the corner telling you how many stars you have.
[link to their image] (https://www.kirupa.com/developer/actionscript/images/textBox_settings.JPG)((((i cant post images yet as i dont have enough points))))
but my version of adobe animate doesnt seem to have the var option! so how do i set up the text?
Try some logic like below. The code is untested, but should be useful to you towards a working solution:
var star :int = 0;
//# create an *enterFrame* function for multiple stars
overworldMc.starMc1.addEventListener( Event.ENTER_FRAME, myClipEvent );
overworldMc.starMc2.addEventListener( Event.ENTER_FRAME, myClipEvent );
overworldMc.starMc3.addEventListener( Event.ENTER_FRAME, myClipEvent );
function myClipEvent( myEvt:Event ) :void
{
//# or... myEvt.currentTarget
if(myEvt.target.hitTestObject(linkMc.hitBoxMc))
{
star += 1; //can be... star++;
myEvt.target.alpha = 0; //# also test replacing *target* with *currentTarget*
//# use String( xxx ) to cast Numeric data type into a Text data type
money_box.text = String(star) + " " + "Stars...";
}
}

Cocos2d-x How to create a bottom tab bar

Just starting out hope this isn’t too noob a question
For the game I’m working on I want to have different tabs, the effect of which would be similar to an iOS bottom tab controller as pictured:
Where I’m at a loss is how to create this? Should this all be on one Scene with different layers being turned on and off? Should I be using multiple Scenes?
Seems like keeping this in one scene wouldn’t really scale. How is this normally done, does cocos2d-x have some sort of concept of containers or something?
I would suggest that you have 2 layers:
The first layers will deal with the images. I guess you can use
ImageView. and position them with the size that you want.
The second part is a tab menu that can be implemented with a second
layer in which you will add the buttons and the background. for the
buttons use the Button class.
I hope that helps.
bool ScreenController::init(){
if(Layer::init())
{
footer = Footer::create();
footer->setDelegate(this);
this->addChild(footer,3);
auto homeLayer= HomeView::create(psize,this);
homeLayer->setPosition(Point(0,footer->getBoundingBox().getMaxY()));
// homeLayer->setAllApiFireTo(this);
auto rakeLayer = RakeView::create(psize);
rakeLayer->setPosition(Point(0,footer->getBoundingBox().getMaxY()));
rakeLayer->setDelegate(this);
Tablayers = LayerMultiplex::create();
Tablayers->addLayer(homeLayer);
Tablayers->addLayer(rakeLayer);
this->addChild(Tablayers);
}
}
void ScreenController::TabControllerAction(ViewDisplayType type) {
Tablayers -> switchTo(index);
}
bool Footer::init(){
if(Layer::init())
{
this->setContentSize(Size(constantDeviceSize.width,108*gamescaleY));
// auto btlayer = LayerColor::create(Color4B::RED,this->getContentSize().width,this->getContentSize().height);
//
// this->addChild(btlayer);
SubButton*sb1 = SubButton::create(Size(this->getContentSize().width/5,this->getContentSize().height),"Home.png","Home.png",CC_CALLBACK_2(Footer::callMenuItem, this) );
sb1->setAnchorPoint(Point(0,0));
sb1->setPosition(Point(0,0));
sb1->addTitleText("Home", SFUI_Regular.c_str());
sb1->colorEfact(true);
sb1->setColorbtn(Color3B::WHITE,colorSkyBlue3B);
this->addChild(sb1);
SubButton*sb2 = SubButton::create(Size(this->getContentSize().width/5,this->getContentSize().height),"Balance.png","Balance.png",CC_CALLBACK_2(Footer::callMenuItem, this));
sb2->setAnchorPoint(Point(0,0));
sb2->setPosition(Point(sb2->getBoundingBox().size.width,0));
sb2->addTitleText("Rake", "arial.ttf");
sb2->colorEfact(true);
sb2->setColorbtn(Color3B::WHITE,colorSkyBlue3B);
this->addChild(sb2);
sb1->setDisplayType(myHomeView);
sb2->setDisplayType(myRakeView);
}
void Footer::selectionByController(int tab)
{
auto tmpbar = dynamic_cast<SubButton *>(this -> getChildren().at(tab-1));
callMenuItem(tmpbar, TouchBegan);
}
void Footer::callMenuItem(Ref *sender, TouchEvent event)
{
if(event==TouchBegan)
{
auto sbitem = (SubButton*)sender;
if(event==TouchBegan)
{
sbitem->selected();
if(this->getDelegate())
{
this->getDelegate()->TabControllerAction(sbitem->getDisplayType());/*call to parant*/
}
CCLOG("CLICKED!->%d",sbitem->getTag());
}
for (int i = 0; i < this->getChildren().size(); i++)
{
auto tmpbar = dynamic_cast<SubButton *>(this -> getChildren().at(i));
if (tmpbar != NULL && tmpbar -> getTag() != sbitem -> getTag())
{
tmpbar ->unSelected();
}
}
}
}

ITextSharp - While Rendering HTML in a ColumnText, it's getting disappeared

I am using the ITextSharp for creating a PDF document. As part of creating a document, I have to render a HTML block in a window of a particular page. I am using the below code to render the HTML. Surprisingly it is not rendering the HTML.
I have attached the HTML Here
public override Rectangle Draw(Rectangle curRectangle)
{
try
{
var column = new ColumnText(this.DocRenderer.PdfDocContentByte);
string css = "p {font-family:HELVETICA; font-style:normal; color:black; font-size:10px}"
+ " li {font-family:HELVETICA; font-style:normal; color:black;font-size:10px}";
foreach (var element in XMLWorkerHelper.ParseToElementList(FileContents of guide.html, css))
{
column.AddElement(element);
}
curRectangle = this.SimulateHeight(curRectangle, column);
column.SetSimpleColumn(curRectangle);
column.Go(false);
}
catch (Exception ex)
{
Logger.LogError(ex.Message, ex);
}
return curRectangle;
}
private Rectangle SimulateHeight(Rectangle curRectangle,ColumnText column)
{
float top = 15;
column.SetSimpleColumn(curRectangle.Left, curRectangle.Bottom, curRectangle.Right, top);
int status = column.Go(true);
top = column.YLine-1;
return new Rectangle(curRectangle.Left, curRectangle.Bottom, curRectangle.Right, top);
}
Even if it only is simulating, your line
int status = column.Go(true);
swallows all composite content added to column if all that content fits into the rectangle. (This allows to easily simulate drawing yet more composite content to the same ColumnText.)
Thus, if the status value is ColumnText.NO_MORE_TEXT, you'll have to add it again, e.g. like this:
private iTextSharp.text.Rectangle SimulateHeight(iTextSharp.text.Rectangle curRectangle, ColumnText column)
{
var compositeContent = new List<IElement>(column.CompositeElements);
float top = 15;
column.SetSimpleColumn(curRectangle.Left, curRectangle.Bottom, curRectangle.Right, top);
int status = column.Go(true);
if (ColumnText.NO_MORE_TEXT == status)
{
foreach (var element in compositeContent)
{
column.AddElement(element);
}
}
top = column.YLine - 1;
return new iTextSharp.text.Rectangle(curRectangle.Left, curRectangle.Bottom, curRectangle.Right, top);
}
Actually even for other status values that list may be partially emptied of the top level elements that did fit. In your case, though, there is but one top level element, the div, so if that does not fit completely, it remains.
As an aside, you use a top value located near to the actual bottom of the page, so it actually is the bottom y coordinate. Such a naming is quite misleading.

Cocos2d-x: Hide Sprite in 1s

I'm newbie in Cocos2d-x.
I'm developing a simple game.
I want to move a sprite from the right to the left.
During it moving, i want that sprite hide in a distance in 1s.
Sequence: 1s:visible,1s:invisible,1s:visible
Example: it run from position A to D
Between A and D we have B and C => ( A->B->C->D)
When sprite in A->B it visible, then B->C it'll be hide, then C->D it visible again.
How can i do it?
Thanks for all your helps.
For show/hide with delay you can use this code (I've wrote it right here, so it may not be compiled after simply copy and paste to your project =) )
float delay = 1f;
CCAction* hideAction = CCHide::create();
CCAction* showAction = CCShow::create();
CCActionInterval* showHideAction = CCSequence::create( CCDelayTime::create(delay),
hideAction,
CCDelayTime::create(delay),
showAction);
CCAction* foreverAction = CCRepeatForever::create(showHideAction);
yourNode->runAction(foreverAction);
To move your node(sprite in your case) you can use both CCMoveTo and CCMoveBy action.
For example
float moveDuration = 5f;
CCPoint targetPos = CCPointMake(someX, someY);
CCAction* moveAction = CCMoveTo::create(moveDuration, targetPos);
yourNode->runAction(moveAction);
Try this action
CCHide * hideAction = CCHide::create();
You can use CCSpawn, this can run two actions at the same time.
You can try this code:
CCAction* action = CCSpawn::createWithTwoActions(CCMoveTo::create(1,CCPointMake(x,y)),
CCFadeOut::create(1));
you_sprite->runAction(action);
update:
You can use CCRepeatForever to run fadein/fadeout, and after move action done, stop this forever action.
Here is the code:
CCSequence* move = CCSequence::create(CCMoveTo::create(3, CCPointMake(1, 1)),
CCCallFunc::create(this, callfunc_selector(SomeClass::some_func)),
NULL);
CCRepeatForever* forever = CCRepeatForever::create(CCSequence::create(CCFadeIn::create(1),
CCFadeOut::create(1)
NULL));
your_sprite->runAction(move);
your_sprite->runAction(forever);
Here is the callback function (invoked after move action);
void SomeClass:some_func(){
your_sprite->stopAllActions();
}
Sprite->runAction(Sequence::create(MoveTo::create(1.0f, Vec2(200,200)),Hide::create(),MoveTo::create(1.0f, Vec2(200,400)),Show::create(),NULL));
SpriteName->runAction(Sequence::create(Hide::create(),NULL));
Here only Sprite Hide.

Custom GTK widget in Vala not working

I'm trying to make a custom GTK widget in Vala, but I'm already failing at the very first basic attempt, so I'd like some help in knowing where I'm going wrong. I feel like I must be missing something painstakingly obvious, but I just can't see it.
I have three files with the following contents:
start.vala:
using Gtk;
namespace WTF
{
MainWindow main_window;
int main(string[] args)
{
Gtk.init(ref args);
main_window = new MainWindow();
Gtk.main();
return 0;
}
}
main_window.vala:
using Gtk;
namespace WTF
{
public class MainWindow : Window
{
public MainWindow()
{
/* */
Entry entry = new Entry();
entry.set_text("Yo!");
this.add(entry);
/* */
/*
CustomWidget cw = new CustomWidget();
this.add(cw);
/* */
this.window_position = WindowPosition.CENTER;
this.set_default_size(400, 200);
this.destroy.connect(Gtk.main_quit);
this.show_all();
}
}
}
custom_widget.vala:
using Gtk;
namespace WTF
{
public class CustomWidget : Bin
{
public CustomWidget()
{
Entry entry = new Entry();
entry.set_text("Yo");
this.add(entry);
this.show_all();
}
}
}
As you can see, in main_window.vala, I have two sets of code. One that adds the Entry widget directly, and one that adds my custom widget. If you run the one that adds the Entry widget directly, you get this result:
If you run the one with the custom widget, however, you get this result:
Just for the record, this is the complication command I use:
valac --pkg gtk+-2.0 start.vala main_window.vala custom_widget.vala -o wtf
EDIT:
Following user4815162342's suggestion, I implemented the size_allocate method on my custom Bin widget, like so:
public override void size_allocate(Gdk.Rectangle r)
{
stdout.printf("Size_allocate: %d,%d ; %d,%d\n", r.x, r.y, r.width, r.height);
Allocation a = Allocation() { x = r.x, y = r.y, width = r.width, height = r.height };
this.set_allocation(a);
stdout.printf("\tHas child: %s\n", this.child != null ? "true" : "false");
if (this.child != null)
{
int border_width = (int)this.border_width;
Gdk.Rectangle cr = Gdk.Rectangle()
{
x = r.x + border_width,
y = r.y + border_width,
width = r.width - 2 * border_width,
height = r.height - 2 * border_width
};
stdout.printf("\tChild size allocate: %d,%d ; %d, %d\n", cr.x, cr.y, cr.width, cr.height);
this.child.size_allocate(cr);
}
}
It writes the following in the console:
Size_allocate: 0,0 ; 400,200
Has child: true
Child size allocate: 0,0 ; 400, 200
And the window renders thusly:
GtkBin is an abstract single-child container, typically intended to decorate the child widget in some way, or change its visibility or size. Without some added value, a single-child container would be indistinguishable from the widget it contains and therefore not very useful.
Since GtkBin doesn't know what kind of decorations you will draw around the child, it expects you to implement your own size_allocate. A simple implementation is available in gtk_event_area_size_allocate, a more complex one in gtk_button_size_allocate.
This answer shows a minimal size_allocate implementation in PyGTK which should be straightforward to port to Vala. If you do anything more complex than that, you will need to also implement expose, and possibly other methods, but this will get you started.