How to perform event on flex line objects.so i can able to move or re-size the line using mouse.I am using flexline as shown in below code:-
spark.primitives.Line;
var st:Stroke =new Stroke(0x345654,1,1);
var obj:Line = new Line();
obj.stroke =st;
obj.xFrom =0;
obj.yFrom=0;
obj.xTo = 500;
obj.yTo = 500;
obj.addEventListener =????? //how to resize this line using mouse??
please explain how to addeventlistner on line or give any other idea to achieve this.
It is not possible. Line is not an InteractiveObject.
You can add it to a group and then listen to its events,
or draw a line with graphics on Sprite and listen to sprite events(this is more lightweight).
Or you may add all graphic primitives like Line to group and calculate by code which line appeared under group click.
Related
In short, I have clickable objects with varying colours. I want these colours upon being clicked to appear in my placeholders (there will be 6). I currently have managed to code so that upon clicking any colour it is placed in the first placeholder.
In what way am I able to code to recognise that the first placeholder has been filled and that once filled, the second placeholder should become the target?
Preferably until the 6th has been filled and then stopped, so that the user can see all 6.
I am thinking something like a for loop would be fitting, but I am not sure how to go about it.
So far it's looking something like this:
//Placeholder
var placeHolder1:MovieClip = new MovieClip();
placeHolder1.x = 20;
placeHolder1.y = 245;
stage.addChild(placeHolder1);
//Placeholder2 (UNUSED CURRENTLY)
var placeHolder2:MovieClip = new MovieClip();
placeHolder2.x = 60;
placeHolder2.y = 245;
stage.addChild(placeHolder2);
//Click and select colours
var newBlue:cBlue = new cBlue();
numBlue.addEventListener(MouseEvent.CLICK, fBlue)
function fBlue(e:MouseEvent){
placeHolder1.addChild(newBlue);
}
var newRed:cRed = new cRed();
numRed.addEventListener(MouseEvent.CLICK, fRed)
function fRed(e:MouseEvent){
placeHolder1.addChild(newRed);
}
First, you probably want to learn about Arrays (or Vectors). Arrays/Vectors are lists, so you would put all your placeholders into an array:
var placeHolders:Array = [placeHolder1, placeHolder2];
Though, since there is a formula to your place holder creation, you probably would want to do this in a loop to make it DRYer (Don't Repeat Yourself)
This loop would create 10 place holders and add them to the array:
var placeholders:Array = new Array();
for(var i:int=0; i < 10; i++){
var placeHolder = new Sprite(); //if your not using timelines, just use Sprite instead of MovieClip as it's less overhead
placeHolder.x = 20 * (i + 1); //i starts at 0, and goes to 9
placeHolder1.y = 245;
stage.addChild(placeHolder); //add it to the display list
placeholders.push(placeHolder); //add it to the array
}
Now (continuing to be DRY), attached the same event listener to all your color buttons:
numBlue.addEventListener(MouseEvent.CLICK, selectColor);
numRed.addEventListener(MouseEvent.CLICK, selectColor);
In that event handler I've called 'selectColor' (see code comments)
function selectColor(e:Event):void {
//get the first element in the array
var placeHolder:Sprite = placeholders.shift(); //shift removes the first item from the array, and returns it
placeHolder.addChild(e.currentTarget); //e.currentTarget refers to item that you attached the event listener to, in this case either numBlue or numRed
}
So to summarize, this gets the first placeholder in the array, remove that item from the array, then adds color button that was clicked as a child of that placeholder.
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.
I'm creating a memory like game. I built all the desktop, the cards generation. I now have two cards of each.
I'm trying to do the pairs delete system.
my function to show the color to find looks like this :
private function onClick(e:MouseEvent):void
{
if (vueDos)
{
vueDos = !vueDos;
faceCarte = new Sprite();
faceCarte.graphics.lineStyle(2,0x000000,.5);
faceCarte.graphics.beginFill(clr);
faceCarte.graphics.drawRoundRect(8,8,this.width - 16, this.height - 16, 10,10);
faceCarte.graphics.endFill();
var _t:TextField = new TextField();
_t.selectable = false;
_t.antiAliasType = "advanced";
_t.autoSize = "left";
_t.defaultTextFormat= new TextFormat(maFont.fontName,24,0x000000);
_t.text = couleur;
_t.x = (this.width - _t.width)/2
_t.y = (this.height - _t.height) >> 1;
faceCarte.addChild(_t);
faceCarte.cacheAsBitmap = true;
this.addChild(faceCarte);
}
if(!vueDos)
}
Does it exist a function wich see if the color of the card is visible (faceCarte), and limit the visibles carte to two then removeChild faceCart.
Thank you in advance
You have to make such a function yourself. It'll be better to assign a couleur property to the card itself, instead of putting it into the TextField and forgetting. This way you'll be able to open first card, get that property, then open second card and compare that one's property with what you received, if match, both cards will be removed.
im very new to as3 so i would appreciate any help.
Im trying to make a counter only using the command "for".
im counting on this from 1 to 1000 in steps of 20.
the next step i want to make is to display on the output tab
i already know i can make it with "trace();", but i also want this to be displayed on
the main .swf window, im trying using a dynamic text-field which i named "dyna"
The problem is that, it is only displaying the last number. "1000" or changing very fast that i barely notice, and the last one remains.
var i:int;
for (i = 1; i < 1001; i+=20)
{
trace(i);
//dyna is the name of my dynamic textfiled
dyna.text = i.toString();
//dinamico.text = String(i);
}
-Is there any way to record all the numbers on my dynamic textbox, something like [1,20,40,60,....] horizontally or vertically.
-Or maybe someway to run this from a button step by step.
like [click, 20; click, 40; click 60.....]
Thanks in advance
var i:int;
var str:String="1";
for (i = 20; i < 1001; i+=20)
{
str=str+","+i;
}
dyna.autoSize = TextFieldAutoSize.LEFT;
dyna.text=str;
Output
1,20,40,60,80,100,120,140,160...
Hope it helps
To run this from the button step by step you need a button, a listener attached to the button, a counter available to both button and text field, and a bit of code. The button has to be somewhere on the stage or in your asset, and named somehow, so you can address it by the name. Here it's named yourButton:
var counter:int=0;
yourButton.addEventListener(MouseEvent.CLICK,updateDyna);
function updateDyna(e:MouseEvent):void {
counter+=20;
if (counter>1000) counter=1000;
dyna.text=counter.toString();
}
Here you are, click - 20, click - 40, etc., up to 1000.
I got the following question.
i added the following elements to the stage:
homeBg = new HomeBg();
homeMask = new HomeDrawBg();
addChild(homeBg);
addChild(homeMask);
I allready instantiated them in the beginning of the document. But my problem is the following. the homeBg layer is a image, the homeMask layer is the same image but it has a pencil scetch look. What i want is that wherever i move my mouse, the homemask layer should be erased so the bottom layer becomes visible(only where the mask is erased). So how can i tell the mask layer to erase itself if the mouse is over it?
Answer attempt 2
You can use the blendMode property of a display object to achieve this. Here's the code (tested):
// set the eraser width (diameter)
var eraserWidth:int = 20;
//get the HomeMask library item
var homeMask:HomeMask = new HomeDrawBg();
homeMask.blendMode = BlendMode.LAYER;
addChild(homeMask);
// create the eraser shape
var eraser:Shape = new Shape();
eraser.graphics.beginFill(0x000000);
eraser.graphics.drawCircle(0,0,eraserWidth/2);
eraser.blendMode = BlendMode.ERASE;
homeMask.addChild(eraser);
homeMask.addEventListener(MouseEvent.MOUSE_MOVE,mouseOverMask);
function mouseOverMask(evt:MouseEvent):void
{
eraser.x = homeMask.mouseX;
eraser.y = homeMask.mouseY;
}
There are a couple of important bits here.
Firstly, you must set the blendMode of the thing you want to erase to BlendMode.LAYER.
Secondly (and this is what has tricked me before) your eraser, with BlendMode.ERASE, must be a child of the object you're wanting to erase.
Have a look on the following:http://www.piterwilson.com/personal/2008/05/07/bitmapdata-erasing-in-as3-with-custom-brush-shape/