Flash and game notes: Rolling numbers (2)

Source: Internet
Author: User

In addition to the pixel COPY method, there is also a beginfillbitmap method to fill in the data we want. Now we provide another method to do a simple experiment.

The first method is to inherit bitmap, and the second method is to crop the bitmap before splicing. There are some differences in code. The function of the experiment can basically achieve the desired effect, as for performance, more tests are required.

Bitmap copy has a high pixel efficiency, which increases the memory size by a little. Using Shape and tweenlite, the memory increase speed is also a little bit. I don't know if it is an illusion.

In this regard, you can also try to do more experiments. Recently, it has been quite busy and it takes less time to write a blog to do experiments.

Okay. You can play with it if you are interested.

 

Test code, in which Nums is the material

package{import flash.display.Sprite;import com.greensock.TweenLite;import com.greensock.plugins.TweenPlugin; import com.greensock.plugins.EndArrayPlugin; import flash.events.MouseEvent;import flash.events.Event;public class Main extends Sprite{private var rollText:RollTextField;public function Main(){TweenPlugin.activate([EndArrayPlugin]);rollText=new RollTextField();addChild(rollText);rollText.x=200;rollText.y=200;rollText.init(new nums(0,0),10);stage.addEventListener(MouseEvent.CLICK,onClick); addEventListener(Event.ENTER_FRAME,onRender);}private function onRender(event:Event):void{rollText.text=Math.random()*5000;//removeEventListener(Event.ENTER_FRAME,onRender);}private function onClick(event:MouseEvent):void{rollText.rollText=Math.random()*5000;}}}

Package {import COM. greensock. tweenlite; import COM. greensock. plugins. *; import flash. display. bitmap; import flash. display. bitmapdata; import flash. display. shape; import flash. geom. matrix; import flash. geom. point; import flash. geom. rectangle; import flash. utils. dictionary; import flash. display. sprite; public class rolltextfield extends shape {private var Source: bitmapdata; private var W: Number; private var H: N Umber; private var rect: rectangle; private var PT: point; private var curvalue: Int = 0; private var offx: Int = 0; private var bitmapdata: dictionary; private var matrix: matrix; Public var filterarr: array = ["+", "-"]; Public var endfun: function; Public Function arttextfield () {}/*** @ Param source the length of a digital resource (0123456789) * @ Param Len cropping. The default value is the length of the resource Number * @ Param offx offset of the Image number, sometimes the resource is signed with + 0123456789 or-0123456789, if 0 */Public Function Init (Source: bitmapdata, Len: int, offx: Int = 0): void {This. source = source; this. offx = offx; W = source. width/Len; H = source. height; bitmapdata = New Dictionary (); If (! Rect) {rect = new rectangle (0, 0, W, H);} If (! Pt) {Pt = new point ();} If (! Matrix) {matrix = new matrix () ;}for (var I: Int = 0; I <Len; I ++) {var bit: bitmapdata = new bitmapdata (W, h, true, 0x00ffffff); rect. X = I * w; rect. width = W; rect. height = H; bit. copypixels (source, rect, pt); bitmapdata [I] = bit ;}} public function set text (value: INT): void {If (! Source | value = curvalue) {return;} This. curvalue = value; this. graphics. clear (); var mynewstr: String = string (value); var Len: Int = mynewstr. length; For (var I: Int = 0; I <Len; I ++) {var num: Int = 0; If (filterarr. indexof (mynewstr. charat (I) =-1) {// Filter Array num = int (mynewstr. charat (I) + offx;} If (bitmapdata [num]) {matrix. tx = I * w; this. graphics. beginbitmapfill (bitmapdata [num], matrix, false); this. graphics. d Rawrect (I * w, 0, W, H); this. graphics. endfill () ;}} public function dispose (): void {for each (VAR bit: bitmapdata in bitmapdata) {bit. dispose (); bit = NULL;} public function set rolltext (value: INT): void {If (! Source | value = curvalue) {return;} var myarray: array = [curvalue]; this. curvalue = value; tweenlite. to (myarray, 1, {endarray: [value], onupdate: onupdateview, oncomplete: oncomplete}); function onupdateview (): void {text = myarray [0];} function oncomplete (): void {If (endfun! = NULL) {endfun. Apply (null, null );}}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.