My problem is when is touch at any location of screen the sprite becomes invisible. But i want to do that the sprite should be invisible only when i tap on the sprite.
bool CharacterSelection::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent
{
CCTouch* touch;
CCPoint tap = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
GoatSprite* goat;
goat = (GoatSprite*) goatSpriteObject;
if(touch){
tap = touch->getLocation();
}
if(goat->boundingBox().containsPoint(tap)){
goat->setVisible(false);
}
return true;
}
sorry if there is any mistake in code.
I feel like your code is weird.
you are missing a parenthesis after CCEvent* pEvent
you already have a touch coming in as pTouch no need to create a new CCTouch* touch;
you are using if (touch) when you have done nothing with it, that would be pTouch
you don't even need the if (touch) conditional at all.
Try simplifying to:
cocos2d::CCPoint p = pTouch->getLocation();
cocos2d::CCRect rect = goat->getBoundingBox();
if(rect.containsPoint(p))
{
// you touched it
}
Related
I have this annoying issue that I hope someone might be able to help me with.
I have a mute button that I created and I have another movieclip inside of that button. All I want it to do is when I toggle the mute the movieclip inside will go to the according frame.
However, every time I try to call the movieclip inside of the button, this error comes up:
Access of possibly undefined property mcMuteToggle through a reference with static type flash.display:SimpleButton.
The instance name for the movieclip within is "mcMuteToggle".
Why not make movieClips that act like buttons?? Since I dont think actual button (simpleButton) types can deal with sub-MovieClips (especially if they too have code). Even if possible don't do it, I can predict a mess whereby Button does things it shouldn't do depending on what code you have in those MClips.
Try an alternate button method, just for a test... You didnt show any test code to work with so I will make assumptions..
1) Make a shape (rectangle?) and convert to MovieClip (or if all coded, then addchild shape to new MovieClip). Let's assume you called it mc_testBtn.
2) Make that MC clickable by coding mc_testBtn.buttonMode = true;
3) Add your mcMuteToggle inside the mc_testBtn
(or by code: mc_testBtn.addChild(mcMuteToggle);
Now you can try something like..
mc_testBtn.addEventListener (MouseEvent.CLICK, toggle_Mute );
function toggle_Mute (evt:MouseEvent) : void
{
if ( whatever condition )
{
mc_testBtn.mcMuteToggle.gotoAndStop(2); //go frame 2
}
else
{
mc_testBtn.mcMuteToggle.gotoAndStop(1); //go frame 1
}
}
This is likely due to strict mode. You can either disable it in the ActionScript settings dialog, access it with a different syntax myButton['mcMuteToggle'], or make a class for the symbol that includes a property mcMuteToggle.
You can also check to make sure the symbol is actually on the stage and that clip is actually in the button:
if('myButton' in root) {
// ...
}
if('mcMuteToggle' in myButton) {
// ...
}
i think u just overwrite that codes. You u can use something like this:
var soundOpen:Boolean = true;
var mySound:Sound = new Sound(new URLRequest("Whatever your sound is"));
var mySc:SoundChannel = new SoundChannel();
var mySt:SoundTransform = new SoundTransform();
mySc = mySound.play();
mcMuteToggle.addEventListener(MouseEvent.CLICK, muteOpenSound);
function muteOpenSound(e:MouseEvent):void
{
if(soundOpen == true)
{
mcMuteToggle.gotoAndStop(2);
/*on frame 2 u need to hold ur soundClose buton so ppl can see :)*/
soundOpen = false;
mySt.volume = 0;
mySc.soundTransfrom = st;
}
else
{
mcMuteToggle.gotoAndStop(1);
soundOpen = true;
mySt.volume = 1;
mySc.soundTransfrom = st;
}
}
This is working for me everytime. Hope u can use it well ;)
i've been stuck on this for quite a while,
i'm working with a Main.as and LivrmScreen.as
and basically LivrmScreen.as is a game screen with other movieclip actions going on...
then i have a button on LivrmScreen that i wish to remove LivrmScreen itself and show homeScreen (all the homeScreen functions are in Main.as)
var homeScreen: HomeScreen;
public function LivrmScreen() {
backhomeBtn.addEventListener(MouseEvent.CLICK, onBackhomeBtnClicked);
}
function onBackhomeBtnClicked(evt:MouseEvent) {
homeScreen = new HomeScreen();
stage.addChild(homeScreen);
parent.removeChild(this);
}
this is what i have right now,
i added parent.removeChild because it won't remove when it is just removeChild ...
and because of that i added stage.addChild so that home screen will show up properly.
but then when homeScreen shows up the button's don't actually work ... so its just showing a dead movieclip. why is that???
i also tried to put the onBackhomeBtnClicked function in main.as thinking so that all the homeScreen functions are there and maybe the buttons will work... but in that case i cant even get the screens to remove and add properly
There's a flaw in your code logics. I don't want to go too deep into the OOP principles or tell you to use frameworks that takes care of these kind of things.
Here's a cheap and fast way out:
in Main.as
private function goToLivrScreen(e:Event = null):void
{
var livrScreen:LivrScreen = new LivrScreen();
livrScreen.addEventListener("onClose", onLivrScreenClose);
addChild(livrScreen);
}
private function onLivrScreenClose(e:Event = null):void
{
removeChild(e.currentTarget as DisplayObject);
// add code to show main menu or whatever you wanted
}
in LivrScreen.as
backhomeBtn.addEventListener(MouseEvent.CLICK, onBackhomeBtnClicked);
and handler
private function onBackhomeBtnClicked(evt:MouseEvent):void
{
// remove all listeners from all object that you have so your LivrScreen could be garbage collected
// when done just dispatch the Event that Main.as is waiting for
dispatchEvent(new Event("onClose"));
}
This is a pretty crude way to do it and there can be many improvements, but it will make your code a little cleaner and hopefully, more scalable in case you want to add new screen or change some implementation.
How to block all mouse touch input into my Actionscript 3 Starling game?
Basically I need to ignore all touch events for a set period of time.
If you don't want an object to be touchable, you can disable the "touchable" property. When it's disabled, neither the object nor its children will receive any more touch events.
There is no need to add new display object to prevent touches.
this.touchable = false;
Developed a quick solution! Basically create a Quad that is the size of your screen, and add it to the front most layer.
Add to init() function of front most layer file:
Starling.current.addEventListener('TOUCH_BLOCKER_ENABLE', touchBlockerEnable);
Starling.current.addEventListener('TOUCH_BLOCKER_DISABLE', touchBlockerDisable);
Then define these functions:
private function touchBlockerEnable(e:Event):void
{
if(!_quad)
{
_quad = new Quad(Starling.current.stage.width,Starling.current.stage.height,0xFFE6E6);
_quad.x = 0;
_quad.y = 0;
_quad.alpha = 0.1;
addChild(_quad);
}
}
private function touchBlockerDisable(e:Event):void
{
if(_quad)
{
removeChild(_quad);
_quad = null;
}
}
Call this function to activate the Touch Blocker:
Starling.current.dispatchEvent(new Event('TOUCH_BLOCKER_ENABLE'));
Okay so I'm working in pure as3 (no creative suite or xml). I have a sprite. It draws a large rectangle and then a slightly smaller rectangle. I want to change the color of the slightly smaller rectangle when I hover the mouse over it. Right now though it will either not respond (I can plainly have the mouse over the rectangle and nothing happens) or it is slow to respond. In addition the collision area of the rectangle seems a bit off, ie it responds more frequently when I have the mouse on the upper left corner of the rectangle than when I have the mouse elsewhere on it.
Anyways here's the code I'm using:
public function MAIN()
{
BUTTON_1.graphics.clear();
BUTTON_1.graphics.beginFill(0x000000);
BUTTON_1.graphics.drawRect(188,96,104,24);
BUTTON_1.graphics.endFill();
BUTTON_1.graphics.beginFill(0x0000DC);
BUTTON_1.graphics.drawRect(190,98,100,20);
BUTTON_1.graphics.endFill();
addChild(BUTTON_1);
BUTTON_1.addEventListener(MouseEvent.MOUSE_OVER,MOUSE_OVER_1);
function MOUSE_OVER_1():void
{
removeChild(BUTTON_1);
BUTTON_1.graphics.clear();
BUTTON_1.graphics.beginFill(0x000000);
BUTTON_1.graphics.drawRect(188,96,104,24);
BUTTON_1.graphics.endFill();
BUTTON_1.graphics.beginFill(0x0000A0);
BUTTON_1.graphics.drawRect(190,98,100,20);
BUTTON_1.graphics.endFill();
addChild(BUTTON_1);
}
}
I'm pretty new to as3 so if there's a better way to do this tell me.
this code doesn't seem correct. listener for mouse events takes MouseEvent object as parameter. So your MOUSE_OVER_1 should be like function MOUSE_OVER_1(e:MouseEvent):void.
I ended up using hitTestPoint, like this:
if (BUTTON_1.hitTestPoint(mouseX,mouseY,true))
{
removeChild(BUTTON_1);
removeChild(TEXT_MENU_1);
BUTTON_1.graphics.clear();
BUTTON_1.graphics.beginFill(0x000000);
BUTTON_1.graphics.drawRect(188,96,104,24);
BUTTON_1.graphics.endFill();
BUTTON_1.graphics.beginFill(0x0000A0);
BUTTON_1.graphics.drawRect(190,98,100,20);
BUTTON_1.graphics.endFill();
addChild(BUTTON_1);
addChild(TEXT_MENU_1);
}
else
{
removeChild(BUTTON_1);
removeChild(TEXT_MENU_1);
BUTTON_1.graphics.clear();
BUTTON_1.graphics.beginFill(0x000000);
BUTTON_1.graphics.drawRect(188,96,104,24);
BUTTON_1.graphics.endFill();
BUTTON_1.graphics.beginFill(0x0000DC);
BUTTON_1.graphics.drawRect(190,98,100,20);
BUTTON_1.graphics.endFill();
addChild(BUTTON_1);
addChild(TEXT_MENU_1);
}
I am trying to cancel a tooltip event (I only want it to display when the mouse is hovered over a certain area), and can't seem to figure it out. I tried stopPropagation, preventDefault, and stopImmediatePropagation, but none of them seem to work.
Here the code I am using:
private function toolTipCreateHandler(event:ToolTipEvent):void {
if(event.currentTarget.mouseX < 130) {
var tooltip:PhotoToolTip = new PhotoToolTip();
tooltip.src = event.currentTarget.toolTip;
event.toolTip = tooltip;
}
else {
event.stopImmediatePropagation();
}
}
Any ideas?
Thanks
Step one, add an event listener to detect when the mouse leaves the area you want the tooltip, run a function and title it something like "toolTipDestroyer"
Step two, You need to call the destroy method of the tooltip manager
private function toolTipDestroyer():void {
if (myToolTip) {
ToolTipManager.destroyToolTip(myToolTip);
myToolTip= null;
}
}
Also, just for readability, give your tooltip some other name than "tooltip", you'll find it easier to follow up on your code later. My example names it "myToolTip"