i am new in action script. trying to split a sentence into words and show them in sprite.and also need which sprite i clicked - actionscript

I am trying to split a sentence in to words and show them into sprite()
var sentence:String = "My name is Subhadip.";
var txt:Array = new Array();
var splittedSentence:Array = sentence.split(" ");
var myArraySprites:Array = new Array();
var myTextImage:Array = new Array();
var div = 40;
for (var i:int = 0; i < splittedSentence.length; i++)
{
var v = 300 - (div * i);
//...
txt[i] = new TextField();
txt[i].autoSize = TextFieldAutoSize.CENTER;
txt[i].text = splittedSentence[i];
var format1:TextFormat = new TextFormat();
format1.size = 24;
txt[i].setTextFormat(format1);
trace(txt[i]);
myArraySprites[i] = new Sprite();
myArraySprites[i].graphics.lineStyle(1, 0x000000, 1);
myArraySprites[i].buttonMode = true;
myArraySprites[i].graphics.beginFill( 0xfffffff );
myArraySprites[i].graphics.drawRect(50, v, 150, div);
myTextImage[i] = new BitmapData(100,v,true,0xffffff);
myTextImage[i].draw(txt[i]);
trace(myTextImage[i]);
//myArraySprites[i].graphics.beginBitmapFill(myTextImage[i]);
//myArraySprites[i].graphics.endFill();
myArraySprites[i].addChild(new Bitmap(myTextImage[i]));
addChild(myArraySprites[i]);
myArraySprites[i].addEventListener(MouseEvent.CLICK, removeThis);
}
function removeThis(e:MouseEvent):void
{
var clickTarget:int = myArraySprites.indexOf(e.currentTarget);
trace("Clicked sprite (id): " + clickTarget);
}
[enter image description here][1]
trying to split a sentence into words and show them in sprite.and also need which sprite i clicked
[1]: https://i.stack.imgur.com/RzHUQ.png

The problem is that you're dealing with screen positions in two different places:
myArraySprites[i].graphics.drawRect(50, v, 150, div);
and
myTextImage[i] = new BitmapData(100,v,true,0xffffff);
Think of it in a more object-oriented way. Each Sprite instance you want to be clickable is a single object. As such it also holds the main properties e.g. the on-screen position determined by it's x and y properties.
Each of those sprites have equal width & height, so make this a global property.
var wid:Number = 150;
var hei:Number = 40;
If we look at your .png, we can see that you want to have a single word centered inside such a sprite. To make things easier, we can use the .align property of the TextFormat class, set it to "center" and make each TextField 150 x 40 by using the wid and hei properties.
Finally each sprite's vertical screen position is shifted up by hei == 40 pixels. So let's determine a start position:
var yPos:Number = 300;
assign it to a sprite instance:
myArraySprites[i].x=50;
myArraySprites[i].y=yPos;
and decrement it by hei inside the for-loop.
Everything put together:
var yPos:Number = 300;
var wid:Number = 150;
var hei:Number = 40;
var sentence:String = "My name is Subhadip.";
var txt:TextField = new TextField();
txt.width = wid;
txt.height = hei;
var format1:TextFormat = new TextFormat();
format1.size = 24;
format1.align = "center";
var splittedSentence:Array = sentence.split(" ");
var myTextImage:Array = new Array();
for (var i:int = 0; i<splittedSentence.length; i++)
{
txt.text = splittedSentence[i];
txt.setTextFormat(format1);
myArraySprites[i] = new Sprite();
myArraySprites[i].x = 50;
myArraySprites[i].y = yPos;
myArraySprites[i].graphics.lineStyle(1, 0x000000, 1);
myArraySprites[i].buttonMode = true;
myArraySprites[i].graphics.beginFill(0xfffffff);
myArraySprites[i].graphics.drawRect(0, 0, wid, hei);
myTextImage[i] = new BitmapData(wid, hei, true, 0xffffff);
myTextImage[i].draw(txt);
myArraySprites[i].addChild(new Bitmap(myTextImage[i]));
addChild(myArraySprites[i]);
myArraySprites[i].addEventListener(MouseEvent.CLICK, removeThis);
yPos -= hei;
}

Related

Xamarin Android - Oxyplot, hide axis values

I am developing a xamarin android app and I am using oxyplot to display a graph. This is the code of oxyplot
OxyPlot.Axes.CategoryAxis xaxis = new OxyPlot.Axes.CategoryAxis();
xaxis.Position = AxisPosition.Bottom;
xaxis.TextColor = OxyColors.Transparent;
xaxis.IsPanEnabled = false;
xaxis.IsAxisVisible = false;
xaxis.MinorTickSize = 0;
xaxis.MajorGridlineStyle = LineStyle.None;
xaxis.MinorGridlineStyle = LineStyle.None;
xaxis.IsZoomEnabled = false;
xaxis.IsPanEnabled = false;
LinearAxis yaxis = new LinearAxis();
yaxis.Position = AxisPosition.Left;
yaxis.TextColor = OxyColors.Transparent;
yaxis.IsPanEnabled = false;
yaxis.IsAxisVisible = false;
yaxis.MinorTickSize = 0;
yaxis.MajorGridlineStyle = LineStyle.None;
yaxis.MinorGridlineStyle = LineStyle.None;
yaxis.IsZoomEnabled = false;
yaxis.IsPanEnabled = false;
OxyPlot.Series.ColumnSeries s1 = new OxyPlot.Series.ColumnSeries();
//s1.IsStacked = true;
s1.Items.Add(new ColumnItem(100));
s1.Items.Add(new ColumnItem(55));
var model = new PlotModel();
model.Background = OxyColors.White;
model.PlotAreaBorderColor = OxyColors.Transparent;
model.Series.Add(s1);
model.IsLegendVisible = false;
return model;
And this is the output in my phone
The problem is that I wanna hide everything except the two bars. Hide the lines and the values of the axis. Thank you very much.
As said in the documentation:
If no axes are defined, linear axes will be added to the bottom and left.
You have not set these two axes to your model, so it adds two default axes.
You could try to use the following codes to add axes:
//...
//Your other code
//....
var model = new PlotModel();
model.Background = OxyColors.White;
model.PlotAreaBorderColor = OxyColors.Transparent;
//Add axes
model.Axes.Add(xaxis);
model.Axes.Add(yaxis);
model.Series.Add(s1);
model.IsLegendVisible = false;

Automatically Slicing Large TIFF File with Photoshop and Save Slices into TIFF Files?

I have a TIFF file. I want to slice it automatically (specifying the number of slices in horizontal and vertical) and save them into TIFF files (I don't want to change the format to png or ...)
I know that in photoshop you can choose the slice tool>>right click>>Divide Slice>>Save for web
However, the "Save for web" doesn't offer saving in TIFF Format and also I don't think it can work for large file (which is the case here).
Anything that can help (script, plugin) is welcome
With the help of some online codes, I created the script below which is capable of automatically slicing a big TIFF into smaller Tiff files:
#target photoshop
function main(){
if(!documents.length) return;
var dlg=
"dialog{text:'Script Interface',bounds:[100,100,380,290],"+
"panel0:Panel{bounds:[10,10,270,180] , text:'' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"title:StaticText{bounds:[60,10,220,40] , text:'File Chop' ,properties:{scrolling:undefined,multiline:undefined}},"+
"panel1:Panel{bounds:[10,40,250,130] , text:'' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"statictext1:StaticText{bounds:[10,10,111,30] , text:'Accross' ,properties:{scrolling:undefined,multiline:undefined}},"+
"statictext2:StaticText{bounds:[140,10,230,27] , text:'Down' ,properties:{scrolling:undefined,multiline:undefined}},"+
"across:DropDownList{bounds:[10,30,100,50]},"+
"down:DropDownList{bounds:[140,30,230,50]},"+
"saveFiles:Checkbox{bounds:[10,60,230,80] , text:'Save and Close new files?'}},"+
"button0:Button{bounds:[10,140,110,160] , text:'Ok' },"+
"button1:Button{bounds:[150,140,250,160] , text:'Cancel' }}};"
var win = new Window(dlg,'File Chop');
if(version.substr(0,version.indexOf('.'))>9){
win.panel0.title.graphics.font = ScriptUI.newFont("Georgia","BOLD",20);
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [1.00, 1.00, 1.00, 1]);
g.backgroundColor = myBrush;
var myPen =g.newPen (g.PenType.SOLID_COLOR, [1.00, 0.00, 0.00, 1],lineWidth=1);
}
win.center();
for(var i=1;i<31;i++){
win.panel0.panel1.across.add ('item', i);
win.panel0.panel1.down.add ('item', i);
}
win.panel0.panel1.across.selection=0;
win.panel0.panel1.down.selection=0;
var done = false;
while (!done) {
var x = win.show();
if (x == 0 || x == 2) {
win.canceled = true;
done = true;
} else if (x == 1) {
done = true;
{
if(!documents.length)return;
var startRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
doc = app.activeDocument;
app.displayDialogs = DialogModes.NO;
doc.flatten();
var tilesAcross = parseInt(win.panel0.panel1.across.selection.index)+1;
var tilesDown =parseInt(win.panel0.panel1.down.selection.index)+1;
var tileWidth = parseInt(doc.width/tilesAcross);
var tileHeight = parseInt(doc.height/tilesDown);
var SaveFiles = win.panel0.panel1.saveFiles.value;
ProcessFiles(tilesDown,tilesAcross,tileWidth,tileHeight,SaveFiles);
app.preferences.rulerUnits = startRulerUnits;
}
}
}
}
main();
function ProcessFiles(Down,Across,offsetX,offsetY,SaveFiles){
try{
var newName = activeDocument.name.match(/(.*)\.[^\.]+$/)[1];
}catch(e){var newName="UntitledChop"}
var Path='';
try{
Path = activeDocument.path;
}catch(e){Path = "~/Desktop";}
if(SaveFiles){
Path = Folder(decodeURI(Path) +"/FileChop");
if(!Path.exists) Path.create();
}
TLX = 0; TLY = 0; TRX = offsetX; TRY = 0;
BRX = offsetX; BRY = offsetY; BLX = 0; BLY = offsetY;
for(var a = 0; a < Down; a++){
for(var i = 0;i <Across; i++){
var NewFileName = newName +"#"+a+"-"+i;
app.activeDocument.duplicate (NewFileName, true);
activeDocument.selection.select([[TLX,TLY],[TRX,TRY],[BRX,BRY],[BLX,BLY]], SelectionType.REPLACE, 0, false);
executeAction( charIDToTypeID( "Crop" ), undefined, DialogModes.NO );
app.activeDocument.selection.deselect();
if(SaveFiles){
var saveFile = File(decodeURI(Path+"/"+NewFileName+".tiff"));
SaveTIFF(saveFile);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
activeDocument = documents[0];
TLX = offsetX * (i+1) ; TRX = TLX + offsetX; BRX = TRX; BLX = TLX;
}
TLX = 0; TLY = offsetY * (a +1); TRX = offsetX; TRY = offsetY * (a +1);
BRX = offsetX; BRY = TRY + offsetY; BLX = 0; BLY = (offsetY * (a +1)+offsetY);
}
if(SaveFiles){
Path.execute()
}
}
function SaveTIFF(saveFile){
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.embedColorProfile = true;
tiffSaveOptions.alphaChannels = true;
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.NONE;
activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
}

Ipad not showing canvas lines properly

I have created small script for vision testing. I posted part of it on https://jsfiddle.net/jaka_87/bpadyanh/
The theory behind it: Canvas is filled with pattern.In each pattern there are 3 lines drawn. Two black ones with the white one in the middle. The white line has 2*with of one black line. When width of this lines is so small that our eye cant distinguish between these three we see only gray background (even thou the lines are still there).
I tested it on few computers. On most of them it works well (thou i saw some strange patterns on some of the older ones - one column of vertical white lines then 2-3 dark column then white one....) I was assuming that it has do do with the display/graphic card or something similar. I tested it on some some mobile devices. It works fine on my Nexus 7 and Moto G, but not on my Transformer Prime pad (strange pattern like described before - for which again I blame the tablet).
It looks absolutely horrible (by far the worst from all tested) on my Ipad and my friends Iphone. I was expecting the best result there since they are known for very good screens but the results are horrible. When the lines are wide enough its OK but when they get narrower they are merged together to one either black or white line - not shown separately.
Is there any way to fix that so it would work on iOS ??
var povecava2 = "0.04226744186046511";
var izmerjeno =1;
var razmak =3.5;
var contrast = 1;
var canvas = document.getElementById('canvas1');
var ctx = canvas.getContext('2d');
// širina canvasa
ctx.canvas.width = window.innerWidth-23;
ctx.canvas.height = window.innerHeight-70;
var sirinaopto=Math.round(200*izmerjeno*povecava2*razmak);
if(sirinaopto & 1){sirinaopto=sirinaopto;}else{ sirinaopto=sirinaopto+1;} // če je širina optotipa soda ali liha
var enota4 =((0.19892970392*130*izmerjeno*povecava2)/4).toFixed(2); // 1 kotna minuta
var center= Math.round((ctx.canvas.width-(sirinaopto))/2);
// kolkrat gre v višino
var kolkratgre = Math.floor(ctx.canvas.height/(sirinaopto));
var visina2= sirinaopto*kolkratgre;
// kolkrat gre v širino
var kolkratgrehor = Math.ceil(ctx.canvas.width/sirinaopto); if(kolkratgrehor % 2 == 0) { var kolkratgrehor=kolkratgrehor-1; }
var zacetek = (ctx.canvas.width-(kolkratgrehor*sirinaopto))/2;
ctx.rect(0,0,ctx.canvas.width,ctx.canvas.height);
ctx.fillStyle="rgb(140,140,140)";
ctx.fill();
// 90 stopinj
var canvasPattern0 = document.createElement("canvas");
canvasPattern0.width = sirinaopto;
canvasPattern0.height = sirinaopto;
var contextPattern0 = canvasPattern0.getContext("2d");
contextPattern0.mozImageSmoothingEnabled = false;
contextPattern0.imageSmoothingEnabled = false;
contextPattern0.translate((canvasPattern0.width/2)-(enota4*2),(canvasPattern0.width/2)-(10*enota4));
contextPattern0.beginPath();
contextPattern0.globalAlpha = contrast;
contextPattern0.moveTo(enota4/2,0);
contextPattern0.lineTo(enota4/2,20*enota4);
contextPattern0.lineWidth=enota4;
contextPattern0.strokeStyle = 'black';
contextPattern0.stroke();
contextPattern0.closePath();
contextPattern0.beginPath();
contextPattern0.globalAlpha = contrast;
contextPattern0.moveTo(enota4*2,0);
contextPattern0.lineTo(enota4*2,20*enota4);
contextPattern0.lineWidth=enota4*2;
contextPattern0.strokeStyle = 'white';
contextPattern0.stroke();
contextPattern0.closePath();
contextPattern0.beginPath();
contextPattern0.globalAlpha = contrast;
contextPattern0.moveTo(enota4*3.5,0);
contextPattern0.lineTo(enota4*3.5,20*enota4);
contextPattern0.lineWidth=enota4;
contextPattern0.strokeStyle = 'black';
contextPattern0.stroke();
contextPattern0.closePath();
// 0 stopinj
var canvasPattern1 = document.createElement("canvas");
canvasPattern1.width = sirinaopto;canvasPattern1.height = sirinaopto;
var contextPattern1 = canvasPattern1.getContext("2d");
contextPattern1.translate(sirinaopto/2, sirinaopto/2);
contextPattern1.rotate(90*Math.PI/180);
contextPattern1.drawImage(canvasPattern0, sirinaopto*(-0.5), sirinaopto*(-0.5));
contextPattern1.save();
var imagesLoaded = [];
imagesLoaded.push(canvasPattern0);
imagesLoaded.push(canvasPattern1);
var randomPattern = function(imgWidth, imgHeight, areaWidth, areaHeight) {
// either set a defined width/height for our images, or use the first one's
imgWidth = sirinaopto;
imgHeight = sirinaopto;
// restrict the randmoness size by using an areaWidth/Height
areaWidth = ctx.canvas.width;
areaHeight = visina2;
// create a buffer canvas
var patternCanvas = canvas.cloneNode(true);
var patternCtx = patternCanvas.getContext('2d');
patternCanvas.width = areaWidth;
patternCanvas.height = areaHeight;
// var xloops = Math.ceil(areaWidth / imgWidth);
var xloops = Math.ceil(areaWidth / imgWidth); if(xloops % 2 == 0) { var xloops=xloops-1; }
var yloops = Math.ceil(areaHeight / imgHeight);
//alert(xloops);
for (var xpos = 0; xpos < xloops; xpos++) {
for (var ypos = 0; ypos < yloops; ypos++) {
var img = imagesLoaded[Math.floor(Math.random() * imagesLoaded.length)];
patternCtx.drawImage(img, (xpos * imgWidth)+zacetek, (ypos * imgHeight), imgWidth, imgHeight);
}
}
// create a pattern from this randomly created image
return patternCtx.createPattern(patternCanvas, 'repeat');
}
var draw = function() {
//create the random pattern (should be moved out of the draw)
var patt = randomPattern(sirinaopto,sirinaopto);
ctx.fillStyle = patt;
ctx.fillRect(0,0,ctx.canvas.width, visina2)
};
draw();

Autocomplete on phone - AS3

I've made an autocomplete that work very well in the swf file.
very simple, when the user write the first letter, a suggestion is made with words.
I've published my project for IOS.
When I'm trying it on the Iphone, nothing is suggested when I'm typing the first letter.
I have to write the first letter and then clicked on "enter" in order to display the suggestions...
I don't want the users to "validate" in order to have the suggestion but simply by typing a letter.
Do you know what could be the problem ? Anyone can help me ?
Weirdly, I've tried on an Android device, and it's working perfectly well ! (like my swf).
Here is my code :
urlLoader.load(new URLRequest("test.txt"));
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
inputField.addEventListener(KeyboardEvent.KEY_UP, suggest);
function loadComplete(e:Event):void
{
suggestions = e.target.data.split(",");
}
function suggest(e:KeyboardEvent):void
{
suggested = [];
for (var i:int = 0; i < textfields.length; i++)
{
removeChild(textfields[i]);
}
textfields = [];
for (var j:int = 0; j < suggestions.length; j++)
{
if (suggestions[j].indexOf(inputField.text.toLowerCase()) == 0)
{
var term:TextField = new TextField();
term.width = 300;
term.height = 20;
term.x = 70;
term.y = (20 * suggested.length) + 314;
term.border = true;
term.borderColor = 0x353535;
term.background = true;
term.backgroundColor = 0xFF9900;
term.textColor = 0x4C311D;
term.defaultTextFormat = format;
term.addEventListener(MouseEvent.MOUSE_UP, useWord);
term.addEventListener(MouseEvent.MOUSE_OVER, hover);
term.addEventListener(MouseEvent.MOUSE_OUT, out);
term.addEventListener(MouseEvent.CLICK, tellMe);
addChild(term);
textfields.push(term);
suggested.push(suggestions[j]);
term.text = suggestions[j];
}
}
if (inputField.length == 0)
{
suggested = [];
for (var k:int = 0; k < textfields.length; k++)
{
removeChild(textfields[k]);
}
textfields = [];
}
if(e.keyCode == Keyboard.DOWN && currentSelection < textfields.length-1)
{
currentSelection++;
textfields[currentSelection].textColor = 0x4C311D;
}
if(e.keyCode == Keyboard.UP && currentSelection > 0)
{
currentSelection--;
textfields[currentSelection].textColor = 0x4C311D;
}
if(e.keyCode == Keyboard.ENTER)
{
inputField.text = textfields[currentSelection].text;
suggested = [];
for (var l:int = 0; l < textfields.length; l++)
{
removeChild(textfields[l]);
}
textfields = [];
currentSelection = 0;
}
}
function useWord(e:MouseEvent):void
{
inputField.text = e.target.text;
suggested = [];
for (var i:int = 0; i < textfields.length; i++)
{
removeChild(textfields[i]);
}
textfields = [];
}
Thank you
EDIT
Here's my new code with Stagetext
var myTextField:StageText = new StageText();
var stageTextInitOptions:StageTextInitOptions;
var urlLoader:URLLoader = new URLLoader();
var suggestions:Array = new Array();
var suggested:Array = new Array();
var textfields:Array = new Array();
var format:TextFormat = new TextFormat();
var currentSelection:int = -1;
var searchChannel:SoundChannel = new SoundChannel();
myTextField.returnKeyLabel = ReturnKeyLabel.SEARCH;
myTextField.addEventListener(KeyboardEvent.KEY_UP, suggest);
stageTextInitOptions = new StageTextInitOptions(false);
myTextField = new StageText(stageTextInitOptions);
myTextField.softKeyboardType = SoftKeyboardType.DEFAULT;
myTextField.returnKeyLabel = ReturnKeyLabel.DONE;
myTextField.autoCorrect = true;
myTextField.fontSize = 20;
myTextField.color = 0x000000;
myTextField.fontWeight = "bold";
myTextField.stage = this.stage;
myTextField.viewPort = new Rectangle(25, 108, stage.stageWidth-40, 28);
urlLoader.load(new URLRequest("Sports2.txt"));
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
myTextField.addEventListener(KeyboardEvent.KEY_UP, suggest);
KeyboardEvent.KEY_UP, KeyboardEvent.KEY_DOWN, TextEvent.TEXT_INPUT doesn't really works at iOS. KeyboardEvent.KEY_DOWN is dispatching only on some keys. I saw only key "Enter".

How To Set Addapted PassData Between Activities?

i am trying to create a dynamic list of buttons and every button create a passed data to another activity that show the relative buttons detile.
the code i used is here:
for (int i = 0; i < size; i++)
{
temp = new Button(this);
temp.SetText(data[i].Name, TextView.BufferType.Normal); //arbitrary task
numArrey[i] = i;
main_linearScroller.AddView(temp);
tv[i] = temp;
tv[i].SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
tv[i].SetBackgroundResource(Resource.Drawable.textbox_back1);
Display d = WindowManager.DefaultDisplay;
int width = d.Width;
int height = d.Height;
tv[i].SetHeight(10);
tv[i].SetWidth(width);
tv[i].Click += (sender,e)=>
{
var detileListShow = new Intent(this, typeof(DetileListShow));
detileListShow.PutExtra("Data", i);
Console.WriteLine("Starting Activity With Data{0}", i);
StartActivity(detileListShow);
};
every thing is good untile last parag that i setting tv[i].click.
every buttons.PutExtera = 21.
how can i fix it?
i think it's better every buttons can save the value that need to return but i dont know how...

Resources