enter image description here
When I click on the button it will perform the action
help me. it did not work
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("run.plist", "run.png");
const int numberSprite = 6;
auto gameSprite = Sprite::createWithSpriteFrameName("run1.png");
gameSprite->setPosition(100,200);
gameSprite->setScale(2);
this->addChild(gameSprite);
Vector<SpriteFrame*> animFrames;
animFrames.reserve(numberSprite);
animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("run1.png"));
animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("run2.png"));
animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("run3.png"));
animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("run4.png"));
animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("run5.png"));
animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("run6.png"));
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
Animate* animate = Animate::create(animation);
gameSprite->runAction(RepeatForever::create(animate));
auto button = Button::create("play.png");
button->setTitleText("");
button->setScale(0.5);
button->setPosition(visibleSize / 2);
button->addTouchEventListener([&gameSprite](Ref* sender, Widget::TouchEventType type) {
switch (type)
{
case ui::Widget::TouchEventType::BEGAN:
auto moveBy3 = MoveBy::create(30, Vec2(2000, 0));
gameSprite->runAction(moveBy3);
case ui::Widget::TouchEventType::ENDED:
break;
default:
break;
}
});
this->addChild(button, 10);
Related
I am new to react and web. I have an issue when calling the function console shows it but i can not show it in my page.
usePriest = (evt, id) => {
const num = 3;
const { rear,bottom2,bottom3,bottom,player1, player2, player3, player4, mycards } = this.state;
var whichPlayer;
switch(num){
case 1:
whichPlayer = player1[0];
rear[1]=whichPlayer;
rear[0]=card6;
break;
case 2:
whichPlayer = player2[0];
bottom[1]=whichPlayer;
bottom[0]=card6;
break;
case 3:
whichPlayer = player3[0];
bottom2[1]=whichPlayer;
bottom2[0]=card6;
break;
case 4:
whichPlayer = player4[0];
bottom3[1]=whichPlayer;
bottom3[0]=card6;
break;
}
// bottom[1]=whichPlayer;
// bottom[0]=card6;
// console.log(bottom);
}
and i call my function in here
else if (mycards[0]==="/static/media/priest.ae71698d.jpg") {
return ( <div>
<button className="button_card1_use" onClick={(evt) => this.usePriest(evt, 1)}>Use</button>
<button className="button_card1_discard">Discard</button>
<div className="about1"><p>Priest</p>
Player is allowed to see another player's hand. </div></div>
)
}
What should i return in function or do something in order to be able to show it on the page.
You just need to call a setState at the end of the function in order tot reflect the changes -:
usePriest = (evt, id) => {
const num = 3;
// your old code
this.setState({ rear,bottom2,bottom3,bottom,player1, player2, player3, player4, mycards })
}
Actually in JS things get copied by reference, so effectively we are mutating the state directly. This is a better solution for the same -:
usePriest = (evt, id) => {
const num = 3;
const { rear,bottom2,bottom3,bottom,player1, player2, player3, player4, mycards } = this.state;
var whichPlayer;
switch(num){
case 1:
this.setState({whichPlayer: player1[0], rear: [...rear, 0: card6, 1: whichPlayer]});
break;
case 2:
this.setState({whichPlayer: player2[0], bottom: [...bottom, 0: card6, 1: whichPlayer]});
break;
case 3:
this.setState({whichPlayer: player3[0], bottom2: [...bottom2, 0: card6, 1: whichPlayer]});
break;
case 4:
this.setState({whichPlayer: player4[0], bottom3: [...bottom3, 0: card6, 1: whichPlayer]});
break;
}
}
I want to write a Scala interface where I click on an image to draw coloured columns. I would also like to detect if the Enter key is pressed. Here is my tentative code:
def main = {
def top = new MainFrame {
title = "Reactive Swing App"
val label = new Label {
var matRaw, mat = new Mat
mat = Imgcodecs.imread("data/batch/14-ZoneDetection/01-noExtraCol/VISSAGE_115.jp2", Imgcodecs.CV_LOAD_IMAGE_COLOR) // images are stored as 8UC3
Image.printCaracColor("mat", mat)
var buf = toBufferedImage(mat)
icon = new ImageIcon(buf)
listenTo(mouse.clicks) // mouse.clicks is a member of Label, this is why the click position is relative to the label
listenTo(keys)
reactions += {
case MousePressed(_, point, _, _, _) => {
mat.submat(0, mat.rows, point.getX.toInt, point.getX.toInt + 1).setTo(new Scalar(0.0, 255.0, 0.0))
buf = toBufferedImage(mat)
icon = new ImageIcon(buf)
println("Click position, x: " + point.getX + ", y: " + point.getY)
}
}
reactions += {
case KeyPressed(_, Key.Enter, _, _) => {
println("Enter pressed")
}
}
}
contents = label
}
val frame = top
frame.resizable = false
frame.visible = true
}
The case MousePressed works properly and the a green column is properly added to the displayed image. However, the case KeyPressed does not work. Could you help me fix this ?
It works if the label is encapsulated in a panel. The label listens to the mouse and the panel to the keyboard.
I want to set body after move delay, I am searching something like runBlock in sprite-kit.
void MySprite::SpawnSprite( cocos2d::Layer *layer )
{
auto mySprite = Sprite::create();
auto body = PhysicsBody::create();
mySprite->setTexture("MySprite.png");
body->createCircle(arrow->getContentSize().width / 2);
body->setDynamic(false);
mySprite->setPosition( startPoint );
layer->addChild(mySprite);
auto moveTest = MoveTo::create(2, Point(200, 200) );
auto waitAction = DelayTime::create(2);
auto action = Sequence::create(moveTest, waitAction, NULL);//I want to set body after waitAction in sequence(mySprite->setPhysicsBody(body))
mySprite->runAction(action);
}
This is so simple in sprite-kit
runAction(
SKAction.sequence([
action,
SKAction.waitForDuration(2.0),
SKAction.runBlock({ //Set Body }))
])
)
What you need is to create CallFuncN and use it in your sequence like this
Fist a function you need:
void attachBody(Ref* pSender)
{
Sprite* mySprite = (Sprite*)pSender;
mySprite->setPhysiceBody(body);
//you can do whatever you want to do with your sprite here.
}
Now you sequence would be something like this
auto action = Sequence::create(moveTest, waitAction,CallFuncN::create(CC_CALLBACK_1(attachBody,this)), NULL);
void MySprite::SetBody(Sprite *sprite)
{
auto body = PhysicsBody::create();
body->createCircle(arrow->getContentSize().width / 2);
body->setDynamic(false);
sprite->setPhysicsBody(body);
}
void MySprite::SpawnSprite( cocos2d::Layer *layer )
{
auto mySprite = Sprite::create();
mySprite->setTexture("MySprite.png");
mySprite->setPosition( startPoint );
layer->addChild(mySprite);
auto moveTest = MoveTo::create(2, Point(200, 200) );
auto waitAction = DelayTime::create(2);
auto funcCallAction = CallFunc::create([=](){
MySprite::SetBody(mySprite);
});
auto action = Sequence::create(moveTest, waitAction, funcCallAction, NULL);
mySprite->runAction(action);
}
When the user clicks on a link it opens a page, adds some content to the local storage then the page that was just opened is supposed to listen for localStorage events. It doesn't seem to fire as I understand both windows must be opened at the same time for the storage event to fire. So I tried to invoke it manually like this:
//page containing the link to other window
$('.view-btn').click(function () {
var selectionName = $(this).parent().parent().find('td:first').text();
var key = "sel-view-1";
addToStorage(key, selectionName);
});
var addToStorage = function (key, value) {
defineKey(key, value);
var e = $.Event("storage");
e.originalEvent = {
key: key,
newValue: value
};
var windowRef = window.open("Create_a_selection", "_self");
$(windowRef).trigger(e);
};
var defineKey = function (key, value) {
if (localStorage.getItem(key) === null) {
localStorage.setItem(key, value);
} else {
var nr = parseInt(key.substring(key.lastIndexOf('-') + 1)) + 1;
key = 'sel-view-' + nr;
return defineKey(key, value);
}
}
//page that was just opened
$(window).bind('storage', onStorageEvent);
function onStorageEvent(storageEvent) {
console.log('yay');
if (storageEvent.originalEvent) {
var key = storageEvent.originalEvent.key;
var value = storageEvent.originalEvent.newValue;
if (key.indexOf("sel-view") > -1) {
$('#selection-name-label').html(value);
}
}
};
But this still doesn't catch the event... It does however catch it if the page is already opened so I'm wondering if this could have something to do with the page not being loaded yet perhaps?
Any help is appreciated.
Many times when I am being tab to switch fields, some times it entered into a text area where pressing tab indent your text. How can I get out these textareas using keyboard so I can continue switching fields.
...here is a fiddle that does the same.
If the websites themselves are overriding this default/expected behaviour then they are breaking a standard UI feature (and possibly lessening site accessibility - although that may depend on the particular application) and it is really up to the site to implement some kind of alternative. There is no other "built-in" keyboard shortcut to move focus to the "next page element".
If, however, you just want to get focus out of that textarea, then you could perhaps use another shortcut, such as Ctrl+L which moves focus to the address bar. From their you can start TABing again to move focus.
You can use the following to do what you need:
http://postcode.cf/support-tabs-in-text-areas.html
/* Support Tabs within your textarea */
HTMLTextAreaElement.prototype.getCaretPosition = function () { //return the caret position of the textarea
return this.selectionStart;
};
HTMLTextAreaElement.prototype.setCaretPosition = function (position) { //change the caret position of the textarea
this.selectionStart = position;
this.selectionEnd = position;
this.focus();
};
HTMLTextAreaElement.prototype.hasSelection = function () { //if the textarea has selection then return true
if (this.selectionStart == this.selectionEnd) {
return false;
} else {
return true;
}
};
HTMLTextAreaElement.prototype.getSelectedText = function () { //return the selection text
return this.value.substring(this.selectionStart, this.selectionEnd);
};
HTMLTextAreaElement.prototype.setSelection = function (start, end) { //change the selection area of the textarea
this.selectionStart = start;
this.selectionEnd = end;
this.focus();
};
var textarea = document.getElementsByTagName('textarea')[0];
textarea.onkeydown = function(event) {
//support tab on textarea
if (event.keyCode == 9) { //tab was pressed
var newCaretPosition;
newCaretPosition = textarea.getCaretPosition() + " ".length;
textarea.value = textarea.value.substring(0, textarea.getCaretPosition()) + " " + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);
textarea.setCaretPosition(newCaretPosition);
return false;
}
if(event.keyCode == 8){ //backspace
if (textarea.value.substring(textarea.getCaretPosition() - 4, textarea.getCaretPosition()) == " ") { //it's a tab space
var newCaretPosition;
newCaretPosition = textarea.getCaretPosition() - 3;
textarea.value = textarea.value.substring(0, textarea.getCaretPosition() - 3) + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);
textarea.setCaretPosition(newCaretPosition);
}
}
if(event.keyCode == 37){ //left arrow
var newCaretPosition;
if (textarea.value.substring(textarea.getCaretPosition() - 4, textarea.getCaretPosition()) == " ") { //it's a tab space
newCaretPosition = textarea.getCaretPosition() - 3;
textarea.setCaretPosition(newCaretPosition);
}
}
if(event.keyCode == 39){ //right arrow
var newCaretPosition;
if (textarea.value.substring(textarea.getCaretPosition() + 4, textarea.getCaretPosition()) == " ") { //it's a tab space
newCaretPosition = textarea.getCaretPosition() + 3;
textarea.setCaretPosition(newCaretPosition);
}
}
}